代码之家  ›  专栏  ›  技术社区  ›  Mark Swardstrom

用户的未定义方法“密码确认”:使用Cucumber和工厂女孩测试AuthLogic

  •  3
  • Mark Swardstrom  · 技术社区  · 15 年前

    当我单独测试它们时,我的测试工作很好,

    rake cucumber FEATURE = 'path/to/feature'
    

    但当我试图逃跑时

    rake cucumber
    

    它们因以下错误而失败

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

    当我尝试使用factory.build(:user)创建用户时,会出现这种情况。我有一个工厂文件定义了:用户。

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

    我在这方面做了很多搜索,没有发现任何相关的内容,所以我假设这是我正在做的其他事情。

    编辑:

    更多信息-我已经能够在一个简单的例子中重现:

    --------特点

    Background:
      Given user "Bad Admin" with email address "badmin@gmail.com" and password "password" exists with "admin" credentials
      Given user "Good Admin" with email address "goodadmin@gmail.com" 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
    

    ----工厂.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
    

    ----步骤.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
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Voldy    15 年前

    在步骤定义文件的某个位置,应使用 before 步进法:

    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
    

    但您不需要使用工厂来创建用户。以下是我的注册方案和现有用户方案:

      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 "test@mail.ru"
        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