Question

I have a model, which should set itself a uuid when the *after_create* hook gets triggered. Due I'm also using FactoryGirl to create Models inside specs, i'm struggling with the problem how the hook could be tested.

class User < ActiveRecord::Base
  after_create: attach_uuid

  def attach_uuid
    self.uuid = SecureRandom.uuid
  end
end

And my spec looks like:

user = FactoryGirl.build(:user, guid: nil)
user.save!
user.uuid.should_not be_nil

But apparently after the save call the hook and the method *attach_uuid* weren't triggered. Any ideas, how to test this kind of behavior after create?

Was it helpful?

Solution

You should reload your object in rspec

user = FactoryGirl.build(:user, guid: nil)
user.save!
user.reload.uuid.should_not be_nil
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top