CheeseFry CheeseFry - 2 years ago 156
Ruby Question

Factory Girl in MiniTest

I am piggybacking on my earlier question where I have a factory creating a parent and child. I am running my unit tests and have a simple one not passing.

test "invoice can save" do
invoice = build(:invoice)
assert, "Error message: #{invoice.errors.full_messages}"

Returns the following error.

Error message: ["Trips can't be blank"]

But the following code works.

test "invoice can save" do
invoice = create(:invoice)
assert, "Error message: #{invoice.errors.full_messages}"

My understanding is that
should be holding it in memory until save is called. Isn't the first a better test to see if it is successfully saving to the database?

Answer Source

Based on the Factory in your earlier question, you are calling before_create to set the trip. However, you are not creating the object, you're building it.

If you changed that before_create to an after_build it should resolve that persnickety test!

Here's a helpful link to an old article from Thoughtbots about the "callbacks" available in FactoryGirl

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download