Devise Rspec registration controller test failing

2019-06-17 08:23发布

问题:

I have the following route:

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks",
                                     :registrations => 'users/registrations',
                                     :sessions => "users/sessions" }

and the following controller test (registrations_controller_spec.rb):

require File.dirname(__FILE__) + '/../spec_helper'

describe Users::RegistrationsController do
  include Devise::TestHelpers
  fixtures :all
  render_views

  before(:each) do
    @request.env["devise.mapping"] = Devise.mappings[:user]
  end

  describe "POST 'create'" do

    describe "success" do
      before(:each) do
        @attr = { :email => "user@example.com",
                  :password => "foobar01", :password_confirmation => "foobar01", :display_name => "New User" }
      end

      it "should create a user" do
        lambda do
          post :create, :user => @attr
          response.should redirect_to(root_path)
        end.should change(User, :count).by(1)
      end

    end

  end

  describe "PUT 'update'" do
    before(:each) do
      @user = FactoryGirl.create(:user)
      @user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
      sign_in @user
    end

    describe "Success" do

      it "should change the user's display name" do
        @attr = { :email => @user.email, :display_name => "Test", :current_password => @user.password }
        put :update, :id => @user, :user => @attr
        puts @user.errors.messages
        @user.display_name.should == @attr[:display_name]
      end

    end
  end

end

Now, when I run rspec spec I get (what I think) are weird results:

The "should create a user" test passes. The user count has increased by 1.

However, my "should change the user's display name" fails as follows:

1) Users::RegistrationsController PUT 'update' Success should change the user's display name
     Failure/Error: @user.display_name.should == @attr[:display_name]
       expected: "Test"
            got: "Boyd" (using ==)

And the weird bit is that my statement:

puts @user.errors.messages

Renders the following message:

{:email=>["was already confirmed, please try signing in"]}

What's going on? The user is signed in! That's proved by the fact that the Rspec error returned the display_name of "Boyd". And why is it displaying a message looks as though it's related with account confirmation as opposed to updating a user's details?

Any help would be greatly appreciated!

回答1:

This works. Thanks holtkampw for seeing what I wasn't! I put some extra code in there just to double check and everything is well!

it "should change the user's display name" do
  subject.current_user.should_not be_nil
  @attr = { :email => @user.email, :display_name => "Test", :current_password => @user.password }
  puts "Old display name: " + subject.current_user.display_name
  put :update, :id => subject.current_user, :user => @attr
  subject.current_user.reload
  response.should redirect_to(root_path)
  subject.current_user.display_name == @attr[:display_name]
  puts "New display name: " + subject.current_user.display_name
end