In my user model I have a function that generates an account ID for the user. I would like to write a test that creates a user (I'm using FactoryGirl) then checks to ensure the account_id field isn't empty after the user is saved. With my current test I'm getting an error that says the following:
NoMethodError: undefined method `to_not=' for #<RSpec>
user.rb
class User < ActiveRecord::Base
before_create :generate_account_id
private
def generate_account_id
self.account_id = loop do
random_account_id = rand.to_s[2..6]
break random_account_id unless self.class.exists?(account_id: random_account_id)
end
end
end
user_spec.rb
#spec/models/user_spec.rb
require 'spec_helper'
require 'rails_helper'
describe User do
it "has a valid factory" do
user = create(:user, :user)
expect(user).to be_valid
end
it "receives a Account ID on successful create" do
user = FactoryGirl.build(:user)
expect(user.account_id).to_not == nil
end
end