我正在阅读这篇博文:
http://clarkware.com/blog/2007/09/08/how-would-you-test-this
最后看到了这段代码:
describe MenuItemsController, 'Creating a new menu item' do
  before do
    @attributes = {'name' => "Enchilada", 'price' => 4.99}
    @menu_item = mock_model(MenuItem)
    MenuItem.should_receive(:new).with(@attributes).once.
      and_return(@menu_item)
  end
  it 'should redirect to index with a notice on successful save' do
    @menu_item.should_receive(:save).with().once.and_return(true)
    post :create, :menu_item => @attributes
    assigns[:menu_item].should be(@menu_item)
    flash[:notice].should_not be(nil)    
    response.should redirect_to(menu_items_url)
  end
  it 'should re-render new template on failed save' do
    @menu_item.should_receive(:save).with().once.and_return(false)
    post :create, :menu_item => @attributes
    assigns[:menu_item].should be(@menu_item)
    flash[:notice].should be(nil)    
    response.should be_success
    response.should render_template('new')
  end
end
我的印象是最好将每个测试(应该、断言、期望)放在它自己的“它”块中。这段代码放了几个。
是的,它使代码更易于阅读,但是如果例如 line: flash[:notice].should_not be(nil)failed 那么您的结果不会直接指向该测试,对吗?
这里有什么建议?每个测试单独或捆绑一些以帮助可读性?
尼尔