2011-04-27 62 views
4

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它應該有錯誤。

我缺少什麼?

+0

奇怪...你的代碼(複製和粘貼完全相同),給我的偉大工程on Rails的3.0.7和RSpec 2.5.0 ... – 2011-04-27 22:42:31

+0

已更新至3.0.7和nada。這讓我瘋狂! – Steve 2011-04-28 02:35:59

+0

克隆/測試此項目時會得到什麼? git://github.com/stevecastaneda/project.git – Steve 2011-04-28 15:20:10

回答

2

由於您使用的摩卡你的模擬框架,你需要告訴Rspec這(只需將摩卡放入您的Gemfile將是不夠的)。在你spec_helper.rb文件,更改此:

config.mock_with :rspec 

要這樣:

config.mock_with :mocha 

和所有測試將通過。


更多信息:

User型號規格的實際工作完全正常,如果你本身運行它:

rspec spec/models/user_spec.rb 

UsersController規範實際上是一種干擾,這這就是爲什麼爲整個項目運行rspec失敗。

您的控制器規格在您的型號規格之前運行。在您的控制器規格中,您有幾個User.any_instance.stub...調用。您最後的UsersController規範存根valid?爲true。這些不僅適用於您的控制器規格。一旦您點擊User型號規格,由於Rspec不知道您正在使用Mocha,因此對valid?的調用仍然會返回true,因爲存在此殘留。

+0

啊!完全忘了改爲摩卡。謝謝!感謝您解釋用戶控制器中的存根問題。我不知道那些存根被傳入我的模型規範中。 – Steve 2011-04-28 15:53:21

1

您需要

user = new_user(:username => '') 
user.should_not be_valid 

否則不進行驗證添加,和,因此,會是沒有錯誤。

工作對我來說:


require 'active_model' 

def new_user(attributes = {}) 
    attributes[:username] ||= 'foo' 
    attributes[:email] ||= '[email protected]' 
    attributes[:password] ||= 'abc123' 
    attributes[:password_confirmation] ||= attributes[:password] 
    User.new(attributes) 
end 

class User 
    include ActiveModel::Validations 

    attr_accessor :username, :email 

    validates_presence_of :username 
end 

describe User do 
    it "should validate" do 
    new_user(:username => '').should_not be_valid 
    end 
end 

更改此:

validates_format_of :username, :with => /^[-\w\[email protected]]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or [email protected]" 

這樣:

validates_format_of :username, :with => /^[-\w\[email protected]]+$/i, :message => "should only contain letters, numbers, or [email protected]", :unless => lambda {|u| u.username.blank?} 
+0

仍然無法正常工作。 「預計有效?返回假,變得真實」。我很難過! – Steve 2011-04-27 21:22:17

+0

可能是你在回調中做的事情等。更新答案,驗證對我有用。 – Roman 2011-04-27 21:31:13

+0

已更新。謝謝你的幫助。 Hrm - 如果驗證存在是否被allow_blank覆蓋了會怎麼樣?用戶名「」不是空的,對吧?更新:在allow_blanks上更改爲false不會更改測試結果。 – Steve 2011-04-27 21:40:15

1

的罪魁禍首,其實,是你users_controller_spec,而且路線:

User.any_instance.stubs(:valid?).returns(true) 
+0

謝謝!有了dmarkow的解釋,我可以看到這是如何造成問題的。這是通過瑞安貝茨的漂亮的發電機工具生成的認證,所以我沒有按照他的規格切換到摩卡。 – Steve 2011-04-28 15:55:01