0

I have two models, Event and Registration:

class Event < ApplicationRecord
  has_many :registrations
end
class Registration < ApplicationRecord
  belongs_to :event
end

I also have fixtures for these models:

event_one:
  [...]
registration_one:
  event: event_one

However, in test, if I get the event and check its registrations, there are none.

event = events(:event_one)
puts event.registrations.count # Prints: 0

How do I make sure these associations are loaded for tests?

RedBassett
  • 3,469
  • 3
  • 32
  • 56
  • Look this post, maybe can help you: [Rails testing - Fixtures for has_many associations](https://stackoverflow.com/questions/24129045/rails-testing-fixtures-for-has-many-associations) – Juanse Gimenez Jan 09 '21 at 06:52
  • The only relevant thing in that post is the recommendation to use DB seeding over fixtures. It doesn't answer my question at all, as I am already declaring the relation in the model that has the foreign key. – RedBassett Jan 09 '21 at 07:14
  • 1
    Does the registration fixture entry have all the fields needed to create a valid registration? – dbugger Jan 09 '21 at 13:04
  • All fields are accounted for in the registration fixture, however as was figured out in the accepted answer, I had IDs mis-assigned in the event fixture to work around another issue, so the ID provided to the registration didn't match the DB. – RedBassett Jan 09 '21 at 18:07

1 Answers1

1

I did a simple example of your code and try solve the problem, the first thinks I found when review the Event and Registration entries is that the Registrations created has event_ids that dont exist in your database.

A solution:

events.yml

event_one:
  id: 111
  name: MyString
  description: MyString

registrations.yml

registration_one:
  name: MyString
  event_id: 111

registration_two:
  name: MyString
  event_id: 111

event_test.rb

require 'test_helper'

class EventTest < ActiveSupport::TestCase
  def setup
    @event = events(:event_one)
  end

  test "the truth" do
    byebug
  end
end

debugger:

(byebug) @event.registrations
#<ActiveRecord::Associations::CollectionProxy [#<Registration id: 357093202, name: "MyString", event_id: 111, created_at: "2021-01-09 17:21:48", updated_at: "2021-01-09 17:21:48">, #<Registration id: 1055835082, name: "MyString", event_id: 111, created_at: "2021-01-09 17:21:48", updated_at: "2021-01-09 17:21:48">]>

I don't think this is the better solution but work.

Juanse Gimenez
  • 481
  • 4
  • 14
  • Ok, so this pointed me to the issue. My events have hardcoded IDs in the fixture file due to another issue I was seeing in test where IDs were not working correctly. As a result, the event ID seen with a fixture reference doesn't match what is in the DB! The correct solution here for me is in fact not to use the `event_id` field in the registration fixture, but to stop the ID hack in the event fixtures and figure that issue out separately. Thanks! – RedBassett Jan 09 '21 at 18:06
  • Np and sorry for my first comment that no resolved your issue – Juanse Gimenez Jan 09 '21 at 18:17