2013-04-15 21 views
0

我想刪除每行主題,它使我的天賦太冗長,有什麼方法可以從rspec代碼的每一行中刪除主題?

我的模型具有下面的代碼: -

形容「#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 => "[email protected]", :sender =>"[email protected]") } 
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 == "[email protected]" 
    subject.gift.sender.should == "[email protected]" 
    subject.payment.payment_amount.should == @order_item1.payment.payment_amount 
    subject.payment.order.should == @cart_group.order 
end 

回答

1

大衛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   => "[email protected]", 
                :sender    =>"[email protected]") } 

     @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 

這樣,你不僅會有更多可讀的規格,它們也會更好地失敗 - 如果有一件事出錯,其他的依然是綠色的。

+0

嘿感謝您的回覆,有沒有辦法從每個塊中刪除@new_item?在「主題」的幫助下,我猜測再次在每個塊中編寫主題將使其變得冗長,應該怎樣寫這種規格而不重複變量/主題的最短路徑 –

+0

'主題'需要一個參數讓您指定一個名稱,而不是'主題' - 所以你可以有像'subject(:item){new_item}; ..... item.cart_group.should == @cart_group ...#etc'。但是請記住,「最短的路」往往不是最可讀的方式! – omnikron

+0

oww謝謝你這麼多omnikron的幫助:)這是我想知道的 –

相關問題