Can @user.update_attributes(params[:user]) fail due to validation?
In addition, you can verify that your test and controller method interact with the same ruby object. It was for me in the past. The way I do this is to stub the find method in the class.
it "email is a new one" do User.stubs(:find).returns(@user) put :update, id: @user, user: FactoryGirl.attributes_for(:user, :email=>" a@b.c ") @user.reload @user.email.should == " a@b.c " puts @user.email end
This ensures that you are talking not only about the same record, but about the same object during the test.
Finally, I would say that your test is very much for you. Basically you are testing update_attributes , which is the main function and fully tested. I would focus on testing controller behavior. Something like that:
let(:user) { FactoryGirl.create(:user) } describe "PUT #update" do before(:each) { User.stubs(:find).returns(user) } it "should redirect to the user path on succesful save" do user.should_receive(:update_attributes).and_return true put :update, user, {} response.should redirect_to(edit_user_path(user)) end it "should render the edit screen again with errors if the model doesn't save" do user.should_receive(:update_attributes).and_return false put :update, user, {} response.should render_template("edit") end end
source share