Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Need to test that user gets a default role upon creation
- # Getting error:
- # User initializing a user has role of "Authenticated"
- # Failure/Error: user = User.create email: 'test@test.com', password: 'testing'
- # ActiveRecord::AssociationTypeMismatch:
- # Role(#2186137620) expected, got NilClass(#2151931040)
- # ./app/models/user.rb:18:in `set_default_role'
- # ./spec/models/user_spec.rb:9:in `block (3 levels) in <top (required)>'
- # app/models/user.rb
- class User < ActiveRecord::Base
- before_create :set_default_role
- has_and_belongs_to_many :roles
- has_many :comments
- # Include default devise modules. Others available are:
- # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
- devise :database_authenticatable, :registerable,
- :recoverable, :rememberable, :trackable, :validatable
- # Setup accessible (or protected) attributes for your model
- attr_accessible :email, :password, :password_confirmation, :remember_me
- def role?(role)
- return !!self.roles.find_by_name(role.to_s.camelize)
- end
- def set_default_role
- self.roles << Role.find_by_name('Authenticated')
- end
- end
- # "spec/models/user_spec.rb"
- require 'spec_helper'
- describe User do
- context "initializing a user" do
- it 'returns a user object' do
- user = User.new email: 'test@test.com', password: 'testing'
- user.should be_a(User)
- end
- it 'has role of "Authenticated"' do
- user = User.create email: 'test@test.com', password: 'testing'
- user.role?('Authenticated').should be_true
- end
- end
- end
Add Comment
Please, Sign In to add comment