我正在使用rspec,rails 3.2.1,并且我在我的Devise“用户”模型上运行测试。
出于某种原因,我所有与长度有关的测试都不起作用(所有测试都失败了),但我已经进行了必要的验证..而且,我的“必需”验证工作正常。
作为一个附带问题,我如何订购验证消息?用户名错误消息显示在底部,即使它是第一个输入框
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :username, :bio, :password, :password_confirmation, :remember_me
username_regex = /\A[\w_]+\z/i
validates :username, :presence => true,
:uniqueness => true,
:format => { :with => username_regex,
:message => "Username can only contain letters, numbers and underscores." },
:length => { :minimum => 3,
:maximum => 15 }
validates :bio, :length => { :maximum => 255 }
end
这是我的测试失败的代码
require 'spec_helper'
describe User do
before(:each) do
@attr = {
:username => "TestUser",
:email => "fake@hell.com",
:password => "123456",
:bio => "This is my bio"
}
end
it "should create a new instance given a valid attribute" do
User.create!(@attr)
end
it "should require a username" do
no_username_user = User.new(@attr.merge(:username => ""))
no_username_user.should_not be_valid
end
it "should require an email" do
no_email_user = User.new(@attr.merge(:email => ""))
no_email_user.should_not be_valid
end
it "should reject spaces in username" do
space_username = User.new(@attr.merge(:username => "Test User"))
space_username.should_not be_valid
end
it "should reject usernames that are too long" do
long_username = User.new(@attr.merge(:username => "#{'a'*16}"))
long_username.should_not be_valid
end
it "should reject short bios" do
long_bio = User.new(@attr.merge(:bio => "12"))
long_bio.should_not be_valid
end
it "should reject long bios" do
long_bio = User.new(@attr.merge(:bio => "#{'b'*256}"))
long_bio.should_not be_valid
end
it "should allow no bio" do
no_bio = User.new(@attr.merge(:bio => ""))
no_bio.should be_valid
end
end
答案 0 :(得分:0)
可能是因为您的服务器正在缓存,请尝试重新启动它。