2010-09-30 31 views
3

我的測試工作,當我單獨測試它們罰款,未定義的方法「password_confirmation」用戶:測試Authlogic黃瓜和工廠女孩

rake cucumber FEATURE = 'path/to/feature' 

但是當我嘗試運行

rake cucumber 

他們失敗,出現以下錯誤

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

這出現了,當我嘗試創建一個用戶使用Factory.build (:用戶)。我有一個工廠文件,它定義了一個:user。

我真的不知道如何確認這一點,但它好像authlogic不存在,用戶模型還沒有添加方法。當我在Factory試圖構建一個新用戶時查看User方法(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 

- - 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 

回答

1

某處在你的步驟定義文件,你應該用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 
+0

嗨Voidy - 這是設置黃瓜和authlogic的非常有用的代碼。就我而言,我沒有註冊視圖,用戶是以其他方式創建的。這些用戶本質上是管理員,因此他們是通過控制檯或rake任務創建的。它們是由User.create創建的(:username =>'...',:password =>'...',::password_confirmation =>'...'。我可以想出一個UI來做到這一點,但我寧願不 - 我想弄清楚爲什麼這種方法在一個功能單獨運行時工作,而不是當它們按順序運行時運行。 – Swards 2010-09-30 14:48:23