Guest User

Untitled

a guest
Oct 29th, 2017
85
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: "[email protected]").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. }
  36. end
  37.  
  38. it "has problem with username" do
  39. # Empty username
  40. User.create(@valid_user_attributes.except(:username)).error_on(:username)
  41. .should eql(["can't be blank", "is too short (minimum is 3 characters)"])
  42.  
  43. # Existing username
  44. User.create(@valid_user_attributes.update({username: @user.username})).error_on(:username)
  45. .should eql(["has already been taken"])
  46. end
  47.  
  48. it "it has problem with password" do
  49. # Empty passwords
  50. User.create(@valid_user_attributes.except(:password, :password_confirmation)).error_on(:password)
  51. .should eql(["can't be blank"])
  52.  
  53. # Not matching passwords
  54. User.create(@valid_user_attributes.update({password: 'wrong!'})).error_on(:password)
  55. .should eql(["doesn't match confirmation"])
  56. end
  57.  
  58. it "has problem with email" do
  59. # Empty email
  60. User.create(@valid_user_attributes.except(:email)).error_on(:email)
  61. .should eql(["can't be blank", "is not an email"])
  62.  
  63. User.create(@valid_user_attributes.update({email: "aint_email"})).error_on(:email)
  64. .should eql(["is not an email"])
  65. end
  66. end
  67. end
  68. end
Add Comment
Please, Sign In to add comment