用户通过黄瓜测试中途签出

时间:2011-10-18 16:05:56

标签: ruby-on-rails-3 devise cucumber

我正在将更改密码功能拆分到我网站上的自己的页面中。当我作为真实用户手动运行时,整个过程工作正常,但我无法通过我的Cucumber测试,原因是在最后调用控制器时,current_user神秘地返回nil。

我有一个严重的WTF时刻,因为用户肯定是在测试开始时登录,并且我正在执行与其他通过测试相同的步骤,正如我所说的那样整个工作正常用手踩过它。

以下是有问题的测试:

Scenario: The user should be able to change their password
  Given I have a signed in user with email = "test@mycompany.com" 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();
    });

1 个答案:

答案 0 :(得分:0)

尝试通过Selenium运行它。观察测试运行通常可以提供有关出错的线索。