未定义方法“password_confirmation”适合用户:使用 Cucumber 和 Factory Girl 测试 Authlogic

发布于 2024-09-25 23:54:53 字数 1925 浏览 2 评论 0原文

当我单独测试它们时,我的测试工作正常,

rake cucumber FEATURE = 'path/to/feature'

但是当我尝试运行时,

rake cucumber

它们失败并出现以下错误

undefined method `password_confirmation=' for #<User:0x1069545f0> (NoMethodError)

当我尝试使用 Factory.build(:user) 创建用户时,会出现此错误。我有一个定义 :user 的工厂文件。

我真的不知道如何确认这一点,但好像 authlogic 不存在并且用户模型还没有添加的方法。当我在工厂尝试构建新用户时查看用户方法(User.new.methods.include?(“password_confirmation =”))时,有一个由该名称定义的方法。

我对此进行了大量搜索,但没有看到任何相关内容,所以我假设这是我正在做的其他事情。

编辑:

更多信息 - 我已经能够在一个简单的情况下重现这一点:

------- .feature

Background:
  Given user "Bad Admin" with email address "[email protected]" and password "password" exists with "admin" credentials
  Given user "Good Admin" with email address "[email protected]" and password "password" exists with "edit" credentials

Scenario: valid log in with authorization
  When I go to the login page

Scenario: invalid login
  When I go to the login page

----factorys.rb

Factory.define :user do |u|
  u.first_name "Arthur"
  u.last_name "Dent"
  u.email { Factory.next(:user_email) }
  u.password "default"
  u.password_confirmation "default"
end

----steps.rb

Given /^user "([^"]*)" with email "([^"]*)", password "([^"]*)" and credentials "([^"]*)" exists$/ do |name, email, password, role|
  names = name.split(' ')
  user = Factory(:user, :first_name => names.first, :last_name => names.last, :email => email, :password => password, :password_confirmation => password)

  user.has_role! role.downcase.gsub(' ', '_')  
end

My tests work fine when I'm testing them individually,

rake cucumber FEATURE = 'path/to/feature'

but when I try to run

rake cucumber

They fail with the following error

undefined method `password_confirmation=' for #<User:0x1069545f0> (NoMethodError)

This comes up when I try to create a user using Factory.build(:user). I have a factories file that defines a :user.

I don't really know how to confirm this, but its as though authlogic is not present and the User model does not have the added methods yet. When I look at the User methods (User.new.methods.include?("password_confirmation=")) at the time the Factory tries to build a new user, there is a method defined by that name.

I've done a lot of searching on this, and I don't see anything related, so I'm assuming that it's something else that I am doing.

EDIT:

A little more information - I've been able to reproduce this in a simple case:

------- .feature

Background:
  Given user "Bad Admin" with email address "[email protected]" and password "password" exists with "admin" credentials
  Given user "Good Admin" with email address "[email protected]" and password "password" exists with "edit" credentials

Scenario: valid log in with authorization
  When I go to the login page

Scenario: invalid login
  When I go to the login page

---- factories.rb

Factory.define :user do |u|
  u.first_name "Arthur"
  u.last_name "Dent"
  u.email { Factory.next(:user_email) }
  u.password "default"
  u.password_confirmation "default"
end

---- steps.rb

Given /^user "([^"]*)" with email "([^"]*)", password "([^"]*)" and credentials "([^"]*)" exists$/ do |name, email, password, role|
  names = name.split(' ')
  user = Factory(:user, :first_name => names.first, :last_name => names.last, :email => email, :password => password, :password_confirmation => password)

  user.has_role! role.downcase.gsub(' ', '_')  
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

同尘 2024-10-02 23:54:53

在步骤定义文件中的某处,您应该使用 before 步骤方法激活 Authlogic:

Before do
  include Authlogic::TestCase
  activate_authlogic
end

您的用户工厂应该如下所示:

Factory.define :user do |f|
  f.sequence(:id) { |n| n }
  f.sequence(:username) { |n| "test#{n}" }
  f.password "secret"
  f.password_confirmation { |u| u.password }
  f.sequence(:email) { |n| "test#{n}@example.com" }
end

但您不需要使用 Factory 来创建用户。这是我的注册和现有用户的场景:

  Scenario: Successful sign up
    Given I am an anonymous user
    And I am on the home page
    When I follow "Join now!"
    And I fill in "Username" with "test"
    And I fill in "Email" with "[email protected]"
    And I fill in "Password" with "secret"
    And I fill in "Confirm Password" with "secret"
    And I press "Create my account"
    Then I should be logged in

  Scenario Outline: Show Sign in or Sign out and Sign Up links
    Given the following user records
      | username | password |
      | test     | secret   |
    Given I am logged in as "<login>" with password "secret"
    When I go to the home page
    Then I should <action 1>
    And I should <action 2>
    Examples:
      | login | action 1       | action 2         |
      | test  | see "Sign out" | see "My Profile" |
      |       | see "Sign in"  | see "Sign up"    |

工厂将在第二个场景中使用。相应步骤的定义:

Given /^I am an anonymous user$/ do
  !UserSession.find
end

Then /^I should be logged in$/ do
  UserSession.find
end

Given /^the following (.+) records$/ do |factory, table|
  table.hashes.each do |hash|
    Factory(factory, hash)
  end
end

Given /^I am logged in as "([^\"]*)" with password "([^\"]*)"$/ do |username, password|
  unless username.blank?
    visit signin_url
    fill_in "Username", :with => username
    fill_in "Password", :with => password
    click_button "Sign In"
  end
end

Somewhere in your step definition file you should activate Authlogic with a before step method:

Before do
  include Authlogic::TestCase
  activate_authlogic
end

Your user's factory should look like this one:

Factory.define :user do |f|
  f.sequence(:id) { |n| n }
  f.sequence(:username) { |n| "test#{n}" }
  f.password "secret"
  f.password_confirmation { |u| u.password }
  f.sequence(:email) { |n| "test#{n}@example.com" }
end

But you don't need to use Factory for user creation. Here is my scenario for sign up and for existing user:

  Scenario: Successful sign up
    Given I am an anonymous user
    And I am on the home page
    When I follow "Join now!"
    And I fill in "Username" with "test"
    And I fill in "Email" with "[email protected]"
    And I fill in "Password" with "secret"
    And I fill in "Confirm Password" with "secret"
    And I press "Create my account"
    Then I should be logged in

  Scenario Outline: Show Sign in or Sign out and Sign Up links
    Given the following user records
      | username | password |
      | test     | secret   |
    Given I am logged in as "<login>" with password "secret"
    When I go to the home page
    Then I should <action 1>
    And I should <action 2>
    Examples:
      | login | action 1       | action 2         |
      | test  | see "Sign out" | see "My Profile" |
      |       | see "Sign in"  | see "Sign up"    |

Factory will be used in second scenario. Corresponding step's definitions:

Given /^I am an anonymous user$/ do
  !UserSession.find
end

Then /^I should be logged in$/ do
  UserSession.find
end

Given /^the following (.+) records$/ do |factory, table|
  table.hashes.each do |hash|
    Factory(factory, hash)
  end
end

Given /^I am logged in as "([^\"]*)" with password "([^\"]*)"$/ do |username, password|
  unless username.blank?
    visit signin_url
    fill_in "Username", :with => username
    fill_in "Password", :with => password
    click_button "Sign In"
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文