user模型数据验证测试
This commit is contained in:
parent
70a9a5e552
commit
f24d0e6b54
|
@ -0,0 +1,121 @@
|
|||
require 'spec_helper'
|
||||
describe User do
|
||||
#测试数据验证
|
||||
# before { @user = User.new(login: "ExampleUser" ,mail: "user@example.com",
|
||||
# password: "foobar",password_confirmation: "foobar" ) }
|
||||
before :each do
|
||||
@user = User.new(login: "ExampleUser" ,mail: "user@example.com",
|
||||
password: "foobar",password_confirmation: "foobar" )
|
||||
end
|
||||
subject { @user }#指定@user为测试对象
|
||||
#属性存在性的测试
|
||||
it { should respond_to(:login) }
|
||||
it { should respond_to(:mail) }
|
||||
|
||||
#用户名唯一性的测试
|
||||
describe "when login is already taken" do
|
||||
before do
|
||||
user_with_same_login=@user.dup
|
||||
user_with_same_login.save
|
||||
end
|
||||
it{should_not be_valid}
|
||||
# it "should be valid" do
|
||||
# expect(@user).to be_valid
|
||||
# end
|
||||
end
|
||||
|
||||
#邮箱唯一性测试
|
||||
describe "when mail address is already taken " do
|
||||
before do
|
||||
user_with_same_mail=@user.dup#dup method copy mail
|
||||
user_with_same_mail.mail=@user.mail.upcase#转大写
|
||||
user_with_same_mail.save
|
||||
end
|
||||
it{should_not be_valid}
|
||||
end
|
||||
#邮箱唯一性测试2
|
||||
# describe " is invalid with a same email address" do
|
||||
# User.create(login:'peter',mail:'test@qq.com')#先保存一个合法的对象
|
||||
# user1=User.new(login:'jim',mail:'testqq@.com')#再创建user1作为测试的对比对象
|
||||
# # expect(user1).to have(1).errors_on(:mail)
|
||||
# it{should_not be_valid}
|
||||
# end
|
||||
|
||||
#login长度测试(login最大25字符)
|
||||
describe "when the login is too long" do
|
||||
before{@user.login='a'*25}
|
||||
it{should_not be_valid}
|
||||
end
|
||||
|
||||
#姓和名的长度测试
|
||||
describe "when the first name is too long " do
|
||||
before{@user.firstname='a'*30}
|
||||
it{should_not be_valid}
|
||||
end
|
||||
describe "when the last name is too long " do
|
||||
before{@user.lastname='a'*30}
|
||||
it{should_not be_valid}
|
||||
end
|
||||
|
||||
#login合法性的测试(符合正则表达式规则的用户名:数字英文)
|
||||
describe "when login format is invalid" do
|
||||
it"should be invalid" do
|
||||
username=%w[aa!3 aaa%$&*! 1111==!]
|
||||
username.each do |valid_username|
|
||||
@user.login=valid_username
|
||||
expect(@user).not_to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "when login format is valid" do
|
||||
it"should be valid" do
|
||||
username = %w[jim123 123456 aaaaaa]
|
||||
username.each do |valid_username|
|
||||
@user.login=valid_username
|
||||
expect(@user).not_to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#邮箱合法性测试,在模型user中邮箱使用了正则表达式
|
||||
describe "is the mail valid" do
|
||||
context "when mail format is invalid" do
|
||||
it"should be invalid" do
|
||||
address=%w[user@foo,com user_at_foo.org example.user@foo.foo@bar_baz.com foo@bar+baz.com]
|
||||
address.each do |valid_address|
|
||||
@user.mail=valid_address
|
||||
expect(@user).not_to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context "when mail format is valid" do
|
||||
it"should be valid" do
|
||||
addresses = %w[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
|
||||
addresses.each do |valid_address|
|
||||
@user.mail=valid_address
|
||||
expect(@user).not_to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "when the mail is too long" do#邮箱长度验证的测试
|
||||
before{@user.mail='a'*60}
|
||||
it{should_not be_valid}
|
||||
end
|
||||
|
||||
#验证两次输入的密码是否一致的测试
|
||||
describe "when the password does not match confirmation" do
|
||||
before{@user.password_confirmation="admin123"}
|
||||
it{should_not be_valid}
|
||||
end
|
||||
|
||||
#调用相关方法是否能返回期待的结果
|
||||
#userInfo方法,选择项目成员时显示的用户信息的文字
|
||||
describe "returns the user info when choice the members of the project" do
|
||||
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,38 @@
|
|||
# This file is copied to spec/ when you run 'rails generate rspec:install'
|
||||
ENV["RAILS_ENV"] ||= 'test'
|
||||
require File.expand_path("../../config/environment", __FILE__)
|
||||
require 'rspec/rails'
|
||||
require 'rspec/autorun'
|
||||
|
||||
# Requires supporting ruby files with custom matchers and macros, etc,
|
||||
# in spec/support/ and its subdirectories.
|
||||
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
|
||||
|
||||
RSpec.configure do |config|
|
||||
# ## Mock Framework
|
||||
#
|
||||
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
|
||||
#
|
||||
# config.mock_with :mocha
|
||||
# config.mock_with :flexmock
|
||||
# config.mock_with :rr
|
||||
|
||||
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
||||
config.fixture_path = "#{::Rails.root}/spec/fixtures"
|
||||
|
||||
# If you're not using ActiveRecord, or you'd prefer not to run each of your
|
||||
# examples within a transaction, remove the following line or assign false
|
||||
# instead of true.
|
||||
config.use_transactional_fixtures = true
|
||||
|
||||
# If true, the base class of anonymous controllers will be inferred
|
||||
# automatically. This will be the default behavior in future versions of
|
||||
# rspec-rails.
|
||||
config.infer_base_class_for_anonymous_controllers = false
|
||||
|
||||
# Run specs in random order to surface order dependencies. If you find an
|
||||
# order dependency and want to debug it, you can fix the order by providing
|
||||
# the seed, which is printed after each run.
|
||||
# --seed 1234
|
||||
config.order = "random"
|
||||
end
|
Loading…
Reference in New Issue