moveson
moveson

Reputation: 5213

Rails Fixtures / Uncountable model name results in NoMethodError

I have a model named EventSeries, which is the same singular as it is plural. I have added this in every way I can imagine to the inflector:

inflect.uncountable %w( fish sheep EventSeries event_series Series series )

I have an event_series.yml fixtures file within spec/fixtures. I have even tried adding:

_fixture:
  model_class: EventSeries

at the top of the yml file, but it does not help.

I have also tried changing change the filename to event_serieses.yml and call event_serieses(:d30_short_series), and I get NoMethodError undefined method event_serieses.

I use RSpec for testing. In a system spec, I have the following declaration:

let(:subject_series) { event_series(:d30_short_series) }

When I run the spec, I get this error:

NoMethodError: undefined method `event_series' for <RSpec::MySpecFile>

I have many other models and this pattern works for every other model (using the plural version, like users or events), so I assume this is a pluralization issue. I've searched for answers and found this issue, which indicates the problem can be solved by adding the model name to the inflector, but that has not helped in my case.

I've managed to get all the other inherent problems with uncountable names working; for example, my path helpers are all working properly and Rails find my view files as expected. But I haven't been able to solve this fixture problem.

Is there a way to point RSpec to the correct method to access my fixtures?

Using Rails 5.2, Ruby 2.6.0, and RSpec 3.8.

Upvotes: 3

Views: 386

Answers (1)

Danilo Cabello
Danilo Cabello

Reputation: 2963

Thanks for sharing your open source project, it was simpler to investigate and solve the issue.

The problem with this specific spec is that the needed fixtures were not being loaded.

You have two options to solve this problem.

Option 1: Add :event_series to config.global_fixtures in your rails_helper.rb.

RSpec.configure do |config|
  config.global_fixtures = :a, :event_series, ..., :n

Option 2: Load the fixture just on that spec visit_event_series_spec.rb

RSpec.describe 'visit an event series page' do
  fixtures :event_series

  let(:user) { users(:third_user) }

Then the spec will now fail but for different reasons:

Failures:

  1) visit an event series page when the user is a visitor when all categories are populated Visit the page
     Failure/Error: expect(page).to have_link(resource.send(attr), href: path)
       expected to find visible link "Dirty 30 Running" but there were no matches. Also found "Dirty 30 Running", which matched the selector but not all filters.

Which I believe you have a better understanding than me of why the following event link is not being displayed on the page.

Upvotes: 3

Related Questions