diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6b8c962bd..d400dae80 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,23 +1,24 @@ require 'spec_helper' describe User do - #测试数据验证 - # before { @user = User.new(login: "ExampleUser" ,mail: "user@example.com", - # password: "foobar",password_confirmation: "foobar" ) } + # 测试数据验证 + # 此处采用预构件的方式生成数据 + # it "has a valid facrtory" do + # expect(FactoryGirl.create(:user)).not_to be_valid + # end + before :each do - @user = User.new(login: "ExampleUser" ,mail: "user@example.com", - password: "foobar",password_confirmation: "foobar" ) + @user = User.new(login: 'ExampleUser',firstname: 'sanfeng',lastname: 'zhang' ,mail: 'user@example.com', + password: 'foobar',password_confirmation: 'foobar' ) end - subject { @user }#指定@user为测试对象 - #属性存在性的测试 + + # 指定@user为测试对象 + subject { @user } + # 属性存在性的测试 it { should respond_to(:login) } it { should respond_to(:mail) } - #此处采用与构建的方式生成数据 - # it"is invalid without login" do - # user=FactoryGirl.build(:user,login: nil) - # expect(user).to_not be_valid - # end - #用户名唯一性的测试 + + # 用户名唯一性的测试 describe "when login is already taken" do before do user_with_same_login=@user.dup @@ -29,11 +30,12 @@ describe User do # 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#转大写 + #dup method copy mail + user_with_same_mail=@user.dup + user_with_same_mail.mail=@user.mail.upcase user_with_same_mail.save end it{should_not be_valid} @@ -48,10 +50,10 @@ describe User do #login长度测试(login最大25字符) describe "when the login is too long " do - before{@user.login='a'*25} - it{should be_valid} + before{@user.login='a'*26} + 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} @@ -61,7 +63,7 @@ describe User do it{should_not be_valid} end - #login合法性的测试(符合正则表达式规则的用户名:数字英文) + # login合法性的测试(符合正则表达式规则的用户名:数字英文) describe "when login format is invalid" do it"should be invalid" do username=%w[aa!3 aaa%$&*! 1111==!] @@ -82,7 +84,7 @@ describe User do end end - #邮箱合法性测试,在模型user中邮箱使用了正则表达式 + # 邮箱合法性测试,在模型user中邮箱使用了正则表达式 describe "is the mail valid" do context "when mail format is invalid" do it"should be invalid" do @@ -104,37 +106,39 @@ describe User do end end end - - describe "when the mail is too long" do#邮箱长度验证的测试 + # 邮箱长度验证的测试 + 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 - context "第一种情况" do - @user=User.new() - expect(@user.userInfo).to eq '' - end - context "二种情况" do - @user=User.new() - expect(@user.userInfo).to eq '' - 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' end - # def name - # [firstname, lastname].join(' ') - # end - # it "returns a contact's full name as a string" do - # contact = Contact.new(firstname: 'John', lastname: 'Doe', - # email: 'johndoe@example.com') - # expect(contact.name).to eq 'John Doe'#调用contact的name方法 - # end end \ No newline at end of file