Guest User

Untitled

a guest
Oct 29th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. require 'spec_helper'
  2.  
  3. describe User do
  4. before :each do
  5. @user = Factory :user, password: 'passwd'
  6. end
  7.  
  8. context "#authenticate" do
  9. it 'should find user' do
  10. User.find_by_username(@user.username).try(:authenticate, @user.password).should == @user
  11. User.find_by_email(@user.email).try(:authenticate, @user.password).should == @user
  12. end
  13.  
  14. it "should not find a user" do
  15. # Correct username, but wrong password
  16. User.find_by_username(@user.username).try(:authenticate, "something_else").should be(false)
  17.  
  18. # Wrong username, but correct password
  19. User.find_by_username("jack_sparrow").try(:authenticate, @user.password).should be(nil)
  20. end
  21. end
  22.  
  23. context "#create" do
  24. it "should create user" do
  25. User.create(username: "spiderman", password: "pass",
  26. password_confirmation: "pass", email: "mail@mail.com").should be_instance_of(User)
  27. end
  28.  
  29. context "#should not create user" do
  30. before :all do
  31. @valid_user_attributes = {
  32. username: 'batman',
  33. password: 'pass',
  34. password_confirmation: 'pass',
  35. email: 'mail@mail.com'
  36. }
  37. end
  38.  
  39. it "has problem with username" do
  40. # Empty username
  41. User.create(@valid_user_attributes.except(:username)).error_on(:username)
  42. .should eql(["can't be blank", "is too short (minimum is 3 characters)"])
  43.  
  44. # Existing username
  45. User.create(@valid_user_attributes.update({username: @user.username})).error_on(:username)
  46. .should eql(["has already been taken"])
  47. end
  48.  
  49. it "it has problem with password" do
  50. # Empty passwords
  51. User.create(@valid_user_attributes.except(:password, :password_confirmation)).error_on(:password)
  52. .should eql(["can't be blank"])
  53.  
  54. # Not matching passwords
  55. User.create(@valid_user_attributes.update({password: 'wrong!'})).error_on(:password)
  56. .should eql(["doesn't match confirmation"])
  57. end
  58.  
  59. it "has problem with email" do
  60. # Empty email
  61. User.create(@valid_user_attributes.except(:email)).error_on(:email)
  62. .should eql(["can't be blank", "is not an email"])
  63.  
  64. User.create(@valid_user_attributes.update({email: "aint_email"})).error_on(:email)
  65. .should eql(["is not an email"])
  66. end
  67. end
  68. end
  69. end
Add Comment
Please, Sign In to add comment