RSpec的2.5來說,Rails 3.0.6 - 蹦://github.com/stevecastaneda/project.gitRSpec的和Rails 3 - 簡單的測試失敗
我做了幾個簡單的測試,以確保用戶是有效的註冊時。正在失敗的測試是「應該要求用戶名」。產生的錯誤是:
Failure/Error: new_user(:username => '').should have(1).error_on(:username)
expected 1 error on :username, got 0
user_spec.rb
require File.dirname(__FILE__) + '/../spec_helper'
describe User do
def new_user(attributes = {})
attributes[:username] ||= 'foo'
attributes[:email] ||= '[email protected]'
attributes[:password] ||= 'abc123'
attributes[:password_confirmation] ||= attributes[:password]
User.new(attributes)
end
before(:each) do
User.delete_all
end
it "should be valid" do
new_user.should be_valid
end
it "should require username" do
new_user(:username => '').should have(1).error_on(:username)
end
end
User.rb
class User < ActiveRecord::Base
# new columns need to be added here to be writable through mass assignment
attr_accessible :username, :email, :password, :password_confirmation
attr_accessor :password
before_save :prepare_password
validates_presence_of :username
validates_uniqueness_of :username, :email, :allow_blank => true
validates_format_of :username, :with => /^[-\w\[email protected]]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or [email protected]"
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_presence_of :password, :on => :create
validates_confirmation_of :password
validates_length_of :password, :minimum => 4, :allow_blank => true
# login can be either username or email address
def self.authenticate(login, pass)
user = find_by_username(login) || find_by_email(login)
return user if user && user.password_hash == user.encrypt_password(pass)
end
def encrypt_password(pass)
BCrypt::Engine.hash_secret(pass, password_salt)
end
private
def prepare_password
unless password.blank?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = encrypt_password(password)
end
end
end
正如你可以看到,我只是創建一個新用戶(未使用工廠,只是簡單的測試)與一個空的用戶名,因爲validates_presence_of :username
它應該有錯誤。
我缺少什麼?
奇怪...你的代碼(複製和粘貼完全相同),給我的偉大工程on Rails的3.0.7和RSpec 2.5.0 ... – 2011-04-27 22:42:31
已更新至3.0.7和nada。這讓我瘋狂! – Steve 2011-04-28 02:35:59
克隆/測試此項目時會得到什麼? git://github.com/stevecastaneda/project.git – Steve 2011-04-28 15:20:10