I'm trying to test the set_random_token method seen below from my model
class Invitation < ActiveRecord::Base
has_and_belongs_to_many :users
validates :name, presence: true, uniqueness: true
validates :number_of_uses, presence: true, numericality: true
validates :token, presence: true, uniqueness: true, format: /\A[0-9A-F]+\z/i
before_create :set_random_token
private
def set_random_token
random_hex = SecureRandom.hex(8)
self.token = random_hex
end
end
Here is the piece in my invitation_spec
it 'verifies that the token is set' do
@invitation = Invitation.new
@invitation.save
expect(@invitation.token).not_to be_empty
end
And here is the error I'm getting
2) Invitation verifies that the token is set
Failure/Error: expect(@invitation.token).not_to be_empty
expected to respond to `empty?`
# ./spec/models/invitation_spec.rb:37:in `block (2 levels) in <top (required)>'
I'm pretty new to Rails, so I apologize if the answer is extremely obvious.
save!instead ofsaveto see if that's the case.