我想从每一行中删除主题,这使得我的规范过于冗长,
我的模型有以下代码: -
描述“#clone_for_resubmit”做
let(:new_item) { @order_item1.clone_for_resubmit(:cart_group => @cart_group, :cloned_collections => { }, :order => @cart_group.order, :message => "this is just for testing purpose", :receiver => "example@example.com", :sender =>"ragarwal@kreeti.com" ) }
subject{ new_item}
it "should clone an order_item" do
gift = FactoryGirl.create(:gift, :order_item_id => @order_item1.id, :user => @user)
subject.cart_group.should == @cart_group
subject.collection.name.should == @order_item1.collection.name
subject.collection.book.title.should == @order_item1.collection.book.title
subject.collection.user.should == @order_item1.collection.user
subject.collection.book.book_chapters.count.should == @order_item1.collection.book.book_chapters.count
subject.collection.book.book_chapters.last.chapter_title.should == @order_item1.collection.book.book_chapters.last.chapter_title
subject.collection.items.last.user_recipe.recipe.name.should == @order_item1.collection.items.last.user_recipe.recipe.name
subject.collection.book.book_chapters.last.items.last.user_recipe.recipe.name.should == @order_item1.collection.book.book_chapters.last.items.last.user_recipe.recipe.name
subject.gift.recipient_email.should == gift.recipient_email
subject.gift.message.should == "this is just for testing purpose"
subject.gift.receiver.should == "example@example.com"
subject.gift.sender.should == "ragajhk@vy.com"
subject.payment.payment_amount.should == @order_item1.payment.payment_amount
subject.payment.order.should == @cart_group.order
end
端
答案 0 :(得分:1)
David Chelimsky描述implicit use of 'subject' as a code smell,所以你想要摆脱它是正确的。但是,你的it
块做得过多,无论如何都会大大影响规范的可读性 - 为了使其更具可读性,我建议你做更多类似的事情:
describe Order do
describe '#clone_for_resubmit' do
before do
# @user = ...
# @order_item1 = ...
# @cart_group = ...
@new_item = @order_item1.clone_for_resubmit(:cart_group => @cart_group,
:cloned_collections => { },
:order => @cart_group.order,
:message => "this is just for testing purpose",
:receiver => "example@example.com",
:sender =>"ragarwal@kreeti.com" ) }
@gift = FactoryGirl.create(:gift, :order_item_id => @order_item1.id, :user => @user)
end
it 'should use the correct cart group' do
@new_item.cart_group.should == @cart_group
end
it 'should use the correct collection name' do
@new_item.collection.name.should == @order_item1.collection.name
end
it 'should use the correct book title' do
# etc...
end
end
end
这样,你不仅会有更多可读的规格,它们也会更好地失败 - 如果出现问题,其他的仍然是绿色的。