Controllers are tricky to test, and there's no perfect way to do it. In this episode you will see how I test controllers, and my reasoning behind it.
describe MenuItemsController, "creating a new menu item" do
integrate_views
fixtures :menu_items
it "should redirect to index with a notice on successful save" do
MenuItem.any_instance.stubs(:valid?).returns(true)
post 'create'
assigns[:menu_item].should_not be_new_record
flash[:notice].should_not be_nil
response.should redirect_to(menu_items_path)
end
it "should re-render new template on failed save" do
MenuItem.any_instance.stubs(:valid?).returns(false)
post 'create'
assigns[:menu_item].should be_new_record
flash[:notice].should be_nil
response.should render_template('new')
end
it "should pass params to menu item" do
post 'create', :menu_item => { :name => 'Plain' }
assigns[:menu_item].name.should == 'Plain'
end
end
本文介绍了一种测试控制器的方法,包括创建菜单项后的成功和失败情况处理。通过模拟验证过程,确保了控制器行为符合预期。
714

被折叠的 条评论
为什么被折叠?



