Advertisement
Guest User

Untitled

a guest
Mar 26th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. require 'spec_helper'
  2.  
  3. describe User do
  4.  
  5. before do
  6. @user = User.new(name: "Example User", email: "user@example.com",
  7. password: "foobar", password_confirmation: "foobar")
  8. end
  9.  
  10. subject { @user }
  11. it { should respond_to(:id)}
  12. it { should respond_to(:name) }
  13. it { should respond_to(:email) }
  14. it { should respond_to(:password_digest) }
  15. it { should respond_to(:password) }
  16. it { should respond_to(:password_confirmation) }
  17. it { should respond_to(:remember_token) }
  18. it { should respond_to(:authenticate) }
  19. it { should be_valid }
  20.  
  21.  
  22.  
  23. describe "when name is not present" do
  24. before { @user.name = " " }
  25. it { should_not be_valid }
  26. end
  27.  
  28. describe "when name is too long" do
  29. before { @user.name = "a"*51 }
  30. it { should_not be_valid }
  31. end
  32.  
  33. describe "when email is not present" do
  34. before { @user.email = " " }
  35. it {should_not be_valid}
  36. end
  37.  
  38. describe "when email address is already taken" do
  39. before do
  40. user_with_same_email = @user.dup
  41. user_with_same_email.email = @user.email.upcase
  42. user_with_same_email.save
  43. end
  44. it { should_not be_valid }
  45. end
  46.  
  47. describe "when password is not present" do
  48. before do
  49. @user = User.new(name: "Example User", email: "user@example.com",
  50. password: " ", password_confirmation: " ")
  51. end
  52. it { should_not be_valid }
  53. end
  54.  
  55. describe "when password doesn't match confirmation" do
  56. before { @user.password_confirmation = "mismatch" }
  57. it { should_not be_valid }
  58. end
  59.  
  60. describe "with a password that's too short" do
  61. before { @user.password = @user.password_confirmation = "a" * 5 }
  62. it { should be_invalid }
  63. end
  64.  
  65.  
  66.  
  67. describe "return value of authenticate method" do
  68. before { @user.save }
  69. let(:found_user) { User.find_by(email: @user.email) }
  70. describe "with valid password" do
  71. it { should eq found_user.authenticate(@user.password) }
  72. end
  73. describe "with invalid password" do
  74. let(:user_for_invalid_password) { found_user.authenticate("invalid") }
  75. it { should_not eq user_for_invalid_password }
  76. specify { expect(user_for_invalid_password).to be_false }
  77. end
  78. end
  79.  
  80. describe "remember token" do
  81. before { @user.save }
  82. its(:remember_token) { should_not be_blank }
  83. end
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement