Я пытаюсь очистить свои спецификации, поскольку они становятся чрезвычайно повторяющимися.
У меня есть следующая спецификация
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"