socialforge/spec/models/user_spec.rb

121 lines
3.8 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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