【问题标题】:Invalid password while testing Devise测试设计时密码无效
【发布时间】:2012-01-17 11:12:05
【问题描述】:

我尝试运行测试

describe "CreateFolders" do
  describe "GET /projects" do
    it "new folder", :js => true do
      @user = FactoryGirl.create(:user)
      visit projects_path
      fill_in('user_Login', :with => @user.email)
      fill_in('user_password', :with => @user.password)
      click_button('Sign in')
      page.should have_content('Logged in')
    end
  end

我明白了

Failures:

  1) CreateFolders GET /projects new folder
     Failure/Error: page.should have_content('Logged in')
       expected there to be content "Logged in" in "Invalid email or password.\nSign in\nRemember me\nForgot your password?"                                                                                                                     
     # ./spec/requests/create_folders_spec.rb:17:in `block (3 levels) in <top (required)>'

================================================ ================================ 已更新

FactoryGirl.define do
  factory :user do
    email Faker::Internet.free_email
    password 'password'
    Name Faker::Name.first_name
    Surname Faker::Name.last_name
    Phone Faker::PhoneNumber.phone_number
    Login Faker::Name.first_name
    company {|user| user.association(:company) }
  end
end

【问题讨论】:

  • 你能发布你的工厂定义:user

标签: devise capybara factory-bot


【解决方案1】:

在rails中,共有三个数据库集:开发数据库,​​测试数据库&生产数据库, 您可能正在开发数据库中创建新用户,它应该在测试数据库中创建,因为在您进行测试时,将使用测试数据库。

确保在测试数据库中创建了新用户。

如果您只需要访问需要身份验证的页面,而不需要测试登录表单,这里有一个在 Capybara 中进行的简单方法:https://github.com/plataformatec/devise/wiki/How-To:-Test-with-Capybara

我希望这会有所帮助!

【讨论】:

  • 当我在工厂后调用 User.all 时,我从数据库中获取记录。谢谢你的链接。但是Mysql2::Error: Lock wait timeout exceeded; try restarting transaction: UPDATE users` SET last_sign_in_at = '2012-01-17 13:52:11', current_sign_in_at = '2012-01-17 13:52:11', last_sign_in_ip = '127.0.0.1' , current_sign_in_ip = '127.0.0.1', sign_in_count = 1, updated_at = '2012-01-17 13:52:11' WHERE users.id = 1` 当我使用user = FactoryGirl.create(:user) login_as(user, :scope =&gt; :user)
【解决方案2】:

我假设您正在使用 Capybara 进行测试。尝试将 fill_in 更改为标签名称而不是字段名称。例如:

fill_in "Login", :with => @user.email
fill_in "Password", :with => @user.password

这些对于您的应用程序可能会有所不同 - 只需替换您实际调用的字段标签(当您转到 projects_path 时在屏幕上看到的任何内容)。另外,不需要括号。

【讨论】:

    【解决方案3】:

    你应该为这个目标使用黄瓜

      @javascript
      Scenario Outline: Show or not dasboard
        Given the following user records
          | email                | password  | login    |
          | admin@mail.com       | password  | admin    |
    
        When I am on the home page
        And I fill in "user_email" with "<login>"
        And I fill in "user_password" with "<password>"
        And I press "Sign in"
        Then I should <flash>
    
        Examples:
          | login          | password | flash                         |
          | admin@mail.com | password | see "Signed in successfully." |
    

    【讨论】:

    • 黄瓜跟什么有什么关系。你可以在 rspec 中很好地做到这一点。
    猜你喜欢
    • 2016-02-22
    • 2011-09-23
    • 2016-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    相关资源
    最近更新 更多