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" の display_name を返したという事実によって証明されています。また、ユーザーの詳細を更新するのではなく、アカウントの確認に関連しているように見えるメッセージが表示されるのはなぜですか?

どんな助けでも大歓迎です!

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 に答える