socialforge/spec/models/user_spec.rb

144 lines
4.6 KiB
Ruby
Raw Normal View History

2015-01-23 10:16:46 +08:00
require 'spec_helper'
describe User do
# 测试数据验证
# 此处采用预构件的方式生成数据
# it "has a valid facrtory" do
# expect(FactoryGirl.create(:user)).not_to be_valid
# end
2015-01-23 10:16:46 +08:00
before :each do
@user = User.new(login: 'ExampleUser',firstname: 'sanfeng',lastname: 'zhang' ,mail: 'user@example.com',
password: 'foobar',password_confirmation: 'foobar' )
2015-01-23 10:16:46 +08:00
end
# 指定@user为测试对象
subject { @user }
# 属性存在性的测试
2015-01-23 10:16:46 +08:00
it { should respond_to(:login) }
it { should respond_to(:mail) }
# 用户名唯一性的测试
2015-01-23 10:16:46 +08:00
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
# 邮箱唯一性测试
2015-01-23 10:16:46 +08:00
describe "when mail address is already taken " do
before do
#dup method copy mail
user_with_same_mail=@user.dup
user_with_same_mail.mail=@user.mail.upcase
2015-01-23 10:16:46 +08:00
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字符)
2015-01-24 10:18:32 +08:00
describe "when the login is too long " do
before{@user.login='a'*26}
it{should_not be_valid}
2015-01-23 10:16:46 +08:00
end
# 姓和名的长度测试
2015-01-23 10:16:46 +08:00
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合法性的测试(符合正则表达式规则的用户名:数字英文)
2015-01-23 10:16:46 +08:00
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
2015-01-24 10:18:32 +08:00
expect(@user).to be_valid
2015-01-23 10:16:46 +08:00
end
end
end
# 邮箱合法性测试在模型user中邮箱使用了正则表达式
2015-01-23 10:16:46 +08:00
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
2015-01-23 10:16:46 +08:00
before{@user.mail='a'*60}
it{should_not be_valid}
end
# 验证两次输入的密码是否一致的测试
2015-01-23 10:16:46 +08:00
describe "when the password does not match confirmation" do
before{@user.password_confirmation="admin123"}
it{should_not be_valid}
end
# 选择项目成员时显示的用户信息文字userInfo方法的测试
# describe "when choice the peoject number show the user info" do
# before{ @userwd = User.new(login: 'wudang',firstname: 'sanfeng',lastname: 'zhang' ,mail: 'user@example.com',
# password: 'foobar',password_confirmation: 'foobar' )}
# it "when the firstname and lastname is nil " do
# expect(@userwd.userInfo).to eq 'wudang (zhang sanfeng)'
# end
#
# end
# 返回用户全名的测试
it "return full user's name" do
expect(@user.show_name).to eq 'zhangsanfeng'
end
# 返回匿名用户方法的测试
# User调用其类方法anonymous,返回一个AnonymousUser的实例对象@anonymoususer
# 该对象的lastname属性被赋值为"Anonymous",最后对比其值是否相等
it "return the anonymous user" do
@anonymoususer=AnonymousUser.new
@anonymoususer=User.anonymous
expect(@anonymoususer.lastname).to eq 'Anonymous'
2015-01-23 10:16:46 +08:00
end
end