Tôi có con đường sau:lập mưu RSpec thử nghiệm điều khiển đăng ký thất bại trên bản cập nhật như thể nó đã cố gắng để xác nhận địa chỉ email
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks",
:registrations => 'users/registrations',
:sessions => "users/sessions" }
và thử nghiệm bộ điều khiển sau (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 => "[email protected]",
: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
Bây giờ, khi tôi chạy rspec spec tôi nhận được (những gì tôi nghĩ) là kết quả lạ:
Bài kiểm tra "nên tạo người dùng" vượt qua. Tổng số người dùng đã tăng thêm 1.
Tuy nhiên, tôi "nên thay đổi tên màn hình của người dùng" thất bại như sau:
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 ==)
Và chút lạ là phát biểu của tôi:
puts @user.errors.messages
Cho biết thông báo sau:
{:email=>["was already confirmed, please try signing in"]}
Điều gì đang xảy ra? Người dùng đã đăng nhập! Điều đó được chứng minh bằng thực tế là lỗi Rspec trả về display_name của "Boyd". Và tại sao nó hiển thị một thông điệp trông như thể nó liên quan đến xác nhận tài khoản trái ngược với việc cập nhật chi tiết của người dùng?
Bất kỳ trợ giúp nào sẽ được đánh giá rất nhiều!
Tôi chắc chắn tên được nhận từ nhà máy và không nhất thiết phải là quá trình đăng nhập. Điều này có giúp ích cho bạn hay không: http://stackoverflow.com/questions/4230152/mocks-arent-working-with-rspec-and-devise – holtkampw
Có đúng vậy !!! Không thấy gỗ cho cây! Đã thay thế @user với subject.current_user trong bài kiểm tra và nhận mọi thứ hoạt động. Cảm ơn bạn! – HapiDaze