I have a Message
model with an overloaded initializer, however when creating a fixture for this model, the initializer is never executed and tests thus fail. How can I ensure the initializer gets called?
The only relevant info I found in the documentation is to add self.use_instantiated_fixtures = true
, but regardless of whether I add this or not (to the test itself, to a setup do ... end
block or to test_helper.rb
) the initializer doesn’t get called anyway.
See example below:
# db/schema.rb
create_table "messages", force: :cascade do |t|
t.string "template_name"
t.string "subject"
t.string "body"
# ...
end
# app/models/message.rb
class Message < ApplicationRecord
# ...
def initialize(attributes = nil)
if (template_name = attributes[:template_name])
attributes[:subject] = I18n.t("messages.#{template_name}.subject")
attributes[:body] = I18n.t("messages.#{template_name}.body")
end
super
end
# config/locales/en.yml
messages:
welcome:
subject: "Welcome!"
body: Lorem ipsum dolor sit amet.
The message gets created fine using Message.create
:
Message.create(template_name: "welcome")
=> #<Message:0x00007fa2b2f4ad88
...
template_name: "welcome",
subject: "Welcome!",
body: "Lorem ipsum dolor sit amet.\n",
...>
So now I want to test the instantiation works fine in my tests using a Rails fixture:
# test/fixtures/messages.yml
msg:
template_name: welcome
# test/models/message_test.rb
test "message instantiated with template has subject and body" do
self.use_instantiated_fixtures = true # tried with and without
message = messages(:msg)
assert_equal "Welcome!", message.subject # assertion fails, subject and body are nil
assert_equal "Lorem ipsum dolor sit amet.\n", message.body
end