I'm having issues with getting Rspec to run my after_create hook.
My User model:
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :recoverable, :rememberable,
:trackable, :validatable, :confirmable
has_one :user_profile
after_create :create_user_profile
protected
def create_user_profile
self.user_profile = UserProfile.new(user: self)
self.user_profile.save
end
end
My UserProfile model:
class UserProfile < ActiveRecord::Base
belongs_to :user
has_attached_file :avatar,
styles: { medium: "300x300>", thumb: "100x100>" },
default_url: "/images/:style/missing.png"
validates_attachment :avatar,
content_type: { content_type: "image/jpeg" }
end
My spec:
require 'rails_helper'
RSpec.describe UserProfile, :type => :model do
describe 'initial create' do
before do
@user = User.new(email: '[email protected]',
password: 's3kr3t',
password_confirmation: 's3kr3t')
@user.save
end
it 'should have profile' do
expect(@user.user_profile).to be_valid
end
end
end
Fails with error:
Failures: │~
│~
1) UserProfile initial create should have profile │~
Failure/Error: expect(@user.user_profile).to be_valid │~
NoMethodError: │~
undefined method `valid?' for nil:NilClass │~
# ./spec/models/user_profile_spec.rb:16:in `block (3 levels) in <top (required)>' │~