2012-10-23 20 views
7

कोशिश कर रहा था मैं निम्नलिखित मार्ग है:वसीयत Rspec पंजीकरण नियंत्रक परीक्षण अद्यतन पर असफल रहने के रूप में अगर यह ईमेल पते की पुष्टि करने के लिए

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 => "[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 

अजीब परिणाम अब, जब मैं rspec कल्पना चलाने मैं (मैं क्या सोचता) कर रहे हैं:

"एक उपयोगकर्ता बनाना चाहिए" परीक्षण से गुजरता है। उपयोगकर्ता की संख्या से 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"]} 

क्या हो रहा है: निम्न संदेश renders? उपयोगकर्ता में साइन इन है! यह इस तथ्य से साबित हुआ है कि रुपेक त्रुटि ने "बॉयड" के प्रदर्शन_नाम को वापस कर दिया है। और यह एक संदेश प्रदर्शित क्यों कर रहा है जैसे कि यह उपयोगकर्ता की जानकारी अपडेट करने के विरोध में खाता पुष्टि से संबंधित है?

किसी भी मदद की सराहना की जाएगी!

+0

मुझे यकीन है कि नाम कारखाने से उठाया जा रहा है और आवश्यक रूप से लॉगिन प्रक्रिया नहीं है। क्या यह आपकी मदद करता है: http://stackoverflow.com/questions/4230152/mocks-arent-working-with-rspec-and-devise – holtkampw

+0

हाँ आप सही हैं !!! पेड़ के लिए लकड़ी नहीं देख रहा है! परीक्षण में subject.current_user के साथ @user को बदल दिया है और सबकुछ काम कर रहा है। धन्यवाद! – HapiDaze

उत्तर

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