设计Rspec注册控制器测试失败,就好像它正在尝试确认电子邮件地址一样

Hap*_*aze 7 controller rspec ruby-on-rails registration devise

我有以下路线:

devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks",
                                     :registrations => 'users/registrations',
                                     :sessions => "users/sessions" }
Run Code Online (Sandbox Code Playgroud)

和以下控制器测试(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
Run Code Online (Sandbox Code Playgroud)

现在,当我运行rspec规范时,我得到(我认为)奇怪的结果:

"应创建用户"测试通过.用户数增加了1.

但是,我的"应该更改用户的显示名称"失败如下:

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 ==)
Run Code Online (Sandbox Code Playgroud)

奇怪的是我的声明:

puts @user.errors.messages
Run Code Online (Sandbox Code Playgroud)

呈现以下消息:

{:email=>["was already confirmed, please try signing in"]}
Run Code Online (Sandbox Code Playgroud)

这是怎么回事?用户已登录!事实证明,Rspec错误返回了"Boyd"的display_name.为什么显示一条消息看起来好像与帐户确认相关而不是更新用户的详细信息?

任何帮助将不胜感激!

Hap*_*aze 1

这有效。感谢holtkampw看到了我不知道的东西!我在那里添加了一些额外的代码只是为了仔细检查,一切都很好!

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
Run Code Online (Sandbox Code Playgroud)