0

我想从每一行中删除主题,这使我的规范过于冗长,

我的模型有以下代码:-

描述“#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

结尾

4

1 回答 1

2

David Chelimsky 将“主题”的隐式使用描述为代码气味,因此您想要摆脱它是正确的。但是,您的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

这样,您不仅会有更多可读的规范,而且它们也会更好地失败 - 如果出现问题,其他的仍然是绿色的。

于 2013-04-16T13:22:35.433 回答