Topic: Help with RSpec test on redering 'edit' page after unsuccessful update

I'm coding a small web application which allows users to add some of the goals they have, where goals belong_to user, and a user has_many goals. I'm trying to test the unsuccessful update of a goal, where the 'edit' page is rendered on failure.

The Test:

  describe "PUT 'update'" do
       
    before(:each) do
      @user = test_sign_in(Factory(:user))
      @goal = Factory(:goal, :user => @user)
    end   
       
    describe "failure" do
     
      it "should render the edit page" do
        @goal.update_attributes(:content => "")
        put :update, :id => @goal, :goal => {} 
        response.should render_template('edit')
      end
         
    end
end

The Code:

  def update
    @goal = Goal.find params[:id]
    if @goal.update_attributes params[:goal]
      redirect_to user_path(current_user), :flash => { :success => "Goal updated" }
    else
      @title = "Edit Goal"
      render 'edit'
    end
  end

The Failure:

Failures:

  1) GoalsController PUT 'update' failure should render the edit page
     Failure/Error: response.should render_template('edit')
       Expected block to return true value.
     # ./spec/controllers/goals_controller_spec.rb:97:in `block (4 levels) in <top (required)>'

For some reason I cannot for the life of me get this test to pass. If it helps, I viewed the actual results in the browser, and it's behaving exactly how I'd like. The errors are being rendered and no update is occuring. However, in the debug params at the bottom, it says I am still in the update action. Any help would be appreciated, thanks!

Last edited by grantbachman (2011-05-19 00:33:20)

Re: Help with RSpec test on redering 'edit' page after unsuccessful update

I'm not sure if this is the problem, but you should probably stub out your call to the update_attributes method. Also take out this line in your spec:

@goal.update_attributes(:content => "")

When testing controllers, it's best to avoid hitting the database.

Re: Help with RSpec test on redering 'edit' page after unsuccessful update

Thanks for the advice, however I still can't get the test to pass...

Re: Help with RSpec test on redering 'edit' page after unsuccessful update

Do you have more backtrace info for the error? It's hard to tell where the "Expected block to return true value." part is occurring.

Re: Help with RSpec test on redering 'edit' page after unsuccessful update

Have another try.


____________________
Taylormade R11 irons