#71 Testing Controllers with RSpec

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

你可能感兴趣的:(Flash,rspec)