2012-10-23 16 views
7

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!

+0

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

+0

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

Trả lời

1

Tác phẩm này. Cảm ơn holtkampw vì đã xem những gì tôi không! Tôi đặt thêm một số mã trong đó chỉ để kiểm tra và tất cả mọi thứ là tốt!

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 
Các vấn đề liên quan