ユーザーが作成されたときに、「measurement_units」フィールドにデフォルトの文字列の帝国があることを確認しようとしています。ユーザーを作成してテストすると、望ましい結果が得られますが、仕様でテストすると、「got: nil」が得られます
私の仕様で何が間違っているのか分かりますか?
require 'spec_helper'
require 'cancan/matchers'
describe User do
subject(:user) { User.new(email: 'test@test.com', 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
上記のテストは以下を返します。
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)>'
これはおそらく本当に基本的なことだと思いますが、理解できません。
編集:
次のように、ユーザー モデルのフィルターを作成する前に、最初の Measurement_units を割り当てます。
class User < ActiveRecord::Base
before_create :assign_initial_settings
def assign_initial_settings
self.measurement_units = "imperial"
end