私は次のルートを持っています:
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 を返したという事実によって証明されています。また、ユーザーの詳細を更新するのではなく、アカウントの確認に関連しているように見えるメッセージが表示されるのはなぜですか?
どんな助けでも大歓迎です!