2011-10-18 34 views
1

我正在將更改密碼功能拆分到我自己的網站上。當我以真實用戶的身份手動運行整個過程時,整個過程正常運行,但我無法通過我的Cucumber測試,原因是在最終調用控制器時,current_user神祕地返回nil。用戶通過黃瓜測試中途退出

我有一個嚴重的WTF時刻,因爲用戶DEFINITELY正在登錄在測試的開始,我遵循所有相同的步驟作爲其他通過測試,正如我所說的整個工作正常當手動通過它時。

這裏是有問題的測試:

Scenario: The user should be able to change their password 
    Given I have a signed in user with email = "[email protected]" and password = "password" 
    When I am on the change-password page # this hits registrations_controller#change_password 
    And I fill in "Current password" with "password" 
    And I fill in "New password" with "new_password" 
    And I fill in "Re-enter new password" with "new_password" 
    And I press "Update" 
    Then I should see "You updated your account successfully" 

和登錄的步驟:

Given /^I have a signed in user with email\s*=\s*"([^"]*)" and password\s*=\s*"([^"]*)"$/ do |email, password| 
    @user = User.new 
    @user.email = email 
    @user.password = password 
    @user.confirm! 
    @user.save! 
    visit '/sign_in' 
    fill_in("Email", :with => @user.email) 
    fill_in("Password", :with => @user.password) 
    click_button("Sign in") 
end 

控制器動作,從 「registrations_controller.rb」:

def change_password 
    # calling "current_user" here retuns the logged-in user as expected 
    @user = current_user 
    end 

    def update_password 
    # calling "current_user" here returns nil 
    # error occurs at call to nil.update_with_password 
    @user = current_user 
    if @user.update_with_password(params["user"]) 
     redirect_to after_update_path_for(@user) 
    else 
     clean_up_passwords(@user) 
     render_with_scope :change_password 
    end 
    end 

現在,我們使用設計(1.1.8),我最好的猜測是我做了一些錯誤的設計路線,看起來像這樣在 「routes.rb中」

devise_for :users, :controllers => {:confirmations => "confirmations", :sessions => "sessions", :registrations => "registrations"} do 

    # ...other routes here... 

    get "/change_password", :to => "registrations#change_password" 
    put "/update_password", :to => "registrations#update_password" 
    end 

最後,爲了完整性,這裏是 「change_password.html.haml」

= render :partial => "shared/stats" 

#main 
    = form_for(resource, :as => resource_name, :url => "update_password", :html => { :method => :put, :autocomplete => "off" }) do |f| 
    = devise_error_messages! 
    .edit-account-hold 
     %span Your password 
     = f.label :current_password, "Current password" 
     = f.password_field :current_password 
     = f.label :password, "New password" 
     = f.password_field :password 
     = f.label :password_confirmation, "Re-enter new password" 
     = f.password_field :password_confirmation 

     = f.submit "Update", :class => "orange-button border-radius" 
     = link_to "Cancel", account_path, :id => "cancel-link" 

    :javascript 
    $(document).ready(function() { 
     $("#user_current_password").focus(); 
    }); 
+0

你的控制器裏有'before_filter:authenticate_user!'嗎? – Gazler

+0

嗯,@Gazler,我的在registrations_controller.rb的頂部以下: 'prepend_before_filter:require_no_authentication,:僅=> [:新,:創建] prepend_before_filter:authenticate_scope!:僅=> [:編輯,:update,:destroy]' 我不會說謊,我不知道這些是什麼意思。將調查... –

+0

'authenticate_scope!'看起來像這樣: 'def authenticate_scope! send(:「authenticate _#{resource_name}!」); self.resource = resource_class.find(send(:「current _#{resource_name}」).id); end' 這確實解決對呼叫'發(:的authenticate_user!)' 當我有':在這個回調update_proposal'行動,黃瓜似乎在這調用嘔吐,但它給了我零信息爲什麼。在常規的開發環境中,這不會造成任何問題。 P.S.對不起,我不能在這裏更好地格式化這個東西... –

回答

0

試圖通過硒運行它。觀看測試運行通常可以提供有關錯誤的線索。