2012-08-23 29 views
0

我正試圖用rspec和工廠女孩寫更好的測試。我正在嘗試測試我的用戶模型,但我的第一個測試仍然失敗。我使用Sorcery作爲我的認證系統,但是我找不到太多的如何編寫工廠的例子。我從一個關於魔法github回購的問題得到了線索,但測試仍然失敗。Sorcery User Factory無效

這裏是我的用戶廠:

# spec/factories/users.rb 

FactoryGirl.define do 
    salt = "asdasdastr4325234324sdfds" 
    factory :user do 
    sequence(:username) { |n| "testuser#{n}" } 
    slug    :username 
    email     { "#{username}@example.com" } 
    password    "secret" 
    password_confirmation "secret" 
    salt     salt 
    crypted_password  Sorcery::CryptoProviders::BCrypt.encrypt("secret", salt) 
    admin     false 
    end 
end 

這是模型試驗:

# spec/models/user_spec.rb 
require 'spec_helper' 

describe User do 
    it "has a valid factory" do 
    FactoryGirl.create(:user).should be_valid 
    end 
end 

這裏的用戶模型:

class User < ActiveRecord::Base 
    authenticates_with_sorcery! 

    # Use FriendlyId for better URLs 
    extend FriendlyId 
    friendly_id :username, :use => :slugged 
    attr_accessible :username, :email, :password, :password_confirmation, :weburl, :time_zone, :bio 

    validates_confirmation_of :password 
    validates_presence_of :password, :on =>:create 
    validates_presence_of :username 
    validates_presence_of :email 
    validates_uniqueness_of :username 
    validates_uniqueness_of :email 
end 

這裏是我的錯誤m得到:

1) User has a valid factory 
Failure/Error: FactoryGirl.create(:user).should be_valid 
    expected valid? to return true, got false 
# ./spec/models/user_spec.rb:6:in `block (2 levels) in <top (required)>' 

user_spec.rb的6號線是FactoryGirl.create(:user).should be_valid

我敢肯定,我失去了一些東西。任何人都可以將我指向正確的方向嗎?

+0

你能否包含你的用戶模型代碼,特別是驗證部分? –

+0

我編輯了我的問題以包含用戶模型。 –

回答

0

工廠說用戶無效。要弄清楚爲什麼,請檢查錯誤。

user = Factory.build(:user) 
user.valid? # => false 
user.errors.should be_empty # => fails, and prints the error hash