【发布时间】:2010-04-29 14:43:32
【问题描述】:
我正在尝试清理我的规格,因为它们变得非常重复。
我有以下规格
describe "Countries API" do
it "should render a country list" do
co1 = Factory(:country)
co2 = Factory(:country)
result = invoke :GetCountryList, "empty_auth"
result.should be_an_instance_of(Api::GetCountryListReply)
result.status.should be_an_instance_of(Api::SoapStatus)
result.status.code.should eql 0
result.status.errors.should be_an_instance_of Array
result.status.errors.length.should eql 0
result.country_list.should be_an_instance_of Array
result.country_list.first.should be_an_instance_of(Api::Country)
result.country_list.should have(2).items
end
it_should_behave_like "All Web Services"
it "should render a non-zero status for an invalid request"
end
检查状态的代码块将出现在我针对 50-60 API 的所有规范中。我的第一个想法是将其转移到一个方法中,并且重构肯定会使事情变得更加干燥,如下所示:-
def status_should_be_valid(status)
status.should be_an_instance_of(Api::SoapStatus)
status.code.should eql 0
status.errors.should be_an_instance_of Array
status.errors.length.should eql 0
end
describe "Countries API" do
it "should render a country list" do
co1 = Factory(:country)
co2 = Factory(:country)
result = invoke :GetCountryList, "empty_auth"
result.should be_an_instance_of(Api::GetCountryListReply)
status_should_be_valid(result.status)
result.country_list.should be_an_instance_of Array
result.country_list.first.should be_an_instance_of(Api::Country)
result.country_list.should have(2).items
end
end
这行得通,但是我不禁觉得这不是“正确”的方法,我应该使用共享规范,但是查看定义共享规范的方法我很难看出我将如何重构它使用共享规范的示例。
我将如何使用共享规范来做到这一点,而不必在开始时重新运行相对昂贵的块
co1 = Factory(:country)
co2 = Factory(:country)
result = invoke :GetCountryList, "empty_auth"
【问题讨论】:
标签: ruby-on-rails ruby rspec bdd