ruby-on-rails-3 – RSpec和Rails 3 – 简单测试失败
发布时间:2020-12-17 02:29:11 所属栏目:百科 来源:网络整理
导读:RSpec 2.5,Rails 3.0.6 – git:// github.com/stevecastaneda/project.git 我正在做一些简单的测试,以确保用户在注册时有效.失败的测试是“应该要求用户名”.产生的错误是: Failure/Error: new_user(:username = '').should have(1).error_on(:username) e
RSpec 2.5,Rails 3.0.6 – git://
github.com/stevecastaneda/project.git
我正在做一些简单的测试,以确保用户在注册时有效.失败的测试是“应该要求用户名”.产生的错误是: 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] ||= 'foo@example.com' 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,:allow_blank => true validates_format_of :username,:with => /^[-w._@]+$/i,:allow_blank => true,:message => "should only contain letters,numbers,or .-_@" 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,它应该有错误. 我错过了什么? 解决方法
由于你正在使用Mocha作为你的模拟框架,你需要告诉Rspec这个(只是将Mocha放在你的Gemfile中是不够的).在spec_helper.rb文件中,更改以下内容:
config.mock_with :rspec 对此: config.mock_with :mocha 并且所有测试都将通过. 更多信息: 如果你自己运行它,你的用户模型规范实际上工作得非常好: rspec spec/models/user_spec.rb 您的UsersController规范实际上是一个干扰,这就是为整个项目运行rspec失败的原因. 您的控制器规格在您的型号规格之前运行.在您的控制器规范中,您有几个User.any_instance.stub …调用.你最后的UsersController规范存根有效吗?是真实的.这些不仅适用于您的控制器规格.一旦达到用户型号规范,调用有效?由于Rspec不知道你正在使用Mocha,所以仍然会因为这个存根而返回true. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读