7

我有以下路线:

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

和以下控制器测试(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

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

“应该创建用户”测试通过。用户数增加了 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 ==)

奇怪的是我的声明:

puts @user.errors.messages

呈现以下消息:

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

这是怎么回事?用户已登录!Rspec 错误返回“Boyd”的显示名称这一事实证明了这一点。为什么它显示的消息看起来好像与帐户确认有关,而不是更新用户的详细信息?

任何帮助将不胜感激!

4

1 回答 1

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
于 2012-10-24T19:20:55.480 回答