Guest User

Untitled

a guest
Oct 9th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. # Need to test that user gets a default role upon creation
  2. # Getting error:
  3. # User initializing a user has role of "Authenticated"
  4. # Failure/Error: user = User.create email: 'test@test.com', password: 'testing'
  5. # ActiveRecord::AssociationTypeMismatch:
  6. # Role(#2186137620) expected, got NilClass(#2151931040)
  7. # ./app/models/user.rb:18:in `set_default_role'
  8. # ./spec/models/user_spec.rb:9:in `block (3 levels) in <top (required)>'
  9.  
  10. # app/models/user.rb
  11. class User < ActiveRecord::Base
  12. before_create :set_default_role
  13. has_and_belongs_to_many :roles
  14. has_many :comments
  15. # Include default devise modules. Others available are:
  16. # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  17. devise :database_authenticatable, :registerable,
  18. :recoverable, :rememberable, :trackable, :validatable
  19.  
  20. # Setup accessible (or protected) attributes for your model
  21. attr_accessible :email, :password, :password_confirmation, :remember_me
  22.  
  23. def role?(role)
  24. return !!self.roles.find_by_name(role.to_s.camelize)
  25. end
  26.  
  27. def set_default_role
  28. self.roles << Role.find_by_name('Authenticated')
  29. end
  30. end
  31.  
  32. # "spec/models/user_spec.rb"
  33. require 'spec_helper'
  34. describe User do
  35. context "initializing a user" do
  36. it 'returns a user object' do
  37. user = User.new email: 'test@test.com', password: 'testing'
  38. user.should be_a(User)
  39. end
  40. it 'has role of "Authenticated"' do
  41. user = User.create email: 'test@test.com', password: 'testing'
  42. user.role?('Authenticated').should be_true
  43. end
  44. end
  45. end
Add Comment
Please, Sign In to add comment