我正在尝试与 rspec 和 factory girl 一起编写更好的测试。我正在尝试测试我的用户模型,但我的第一个测试一直失败。我将 Sorcery 用于我的身份验证系统,但我找不到太多关于如何编写工厂的示例。我从巫术 github repo 上的一个问题中得到了线索,但测试仍然失败。
这是我的用户工厂:
# 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
这是我得到的错误:
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
我确定我错过了一些东西。谁能指出我正确的方向?