Reputation: 3938
I'm trying to ensure that when a user is created, the "measurement_units" field has the default string of imperial. When I test by creating a user I get the desired result, but when I test with my spec, I get `got: nil'
Any idea what I'm doing wrong in my spec?
require 'spec_helper'
require 'cancan/matchers'
describe User do
subject(:user) { User.new(email: '[email protected]', password: '12345678', goal_id: '1', experience_level_id: '1', gender: 'female') }
it "should be assigned measurement_units of imperial" do
user.measurement_units.should eq("imperial")
end
end
The above test returns:
1) User should be assigned measurement_units of imperial
Failure/Error: user.measurement_units.should eq("imperial")
expected: "imperial"
got: nil
(compared using ==)
# ./spec/models/user_spec.rb:20:in `block (2 levels) in <top (required)>'
I know this is probably really basic, but I can't figure it out.
EDIT:
I assign the initial measurement_units in a before create filter on the user model like this:
class User < ActiveRecord::Base
before_create :assign_initial_settings
def assign_initial_settings
self.measurement_units = "imperial"
end
Upvotes: 0
Views: 404
Reputation: 25029
Your callback is a before_create
, but you're not actually calling User.create in your test - hence your callback is not getting run.
If you replace the new
with create
you should see the behaviour you expect.
Upvotes: 2