Rails Rspec tests only intermittently recognizing polymorphic associations

40 Views Asked by At

I am in the process of updating a very old legacy app from Rails 3 to Rails 4.2 (I know, I know). Our structure is that we have a models gem, a gem for spec factories, and multiple apps that use both of these gems. I'm currently working in one of these other apps and trying to fix specs.

I've come across this odd problem that I've spent days on. I can't find any answers online, and everything I've come across on Stack Overflow is not fully relatable to this issue.

The issue is this: I have multiple specs failing where a polymorphic association is failing only intermittently. So the association itself is working, but when running the test as a whole file, the polymorphic association fails sometimes. I have a specific file I've been running while working on this that has 3 examples, which are always run randomly, and it's always the second example that fails on this polymorphic association - that calling message.recipient returns that recipient is nil. And again, the first example always passes and the recipient is returned.

Here's the setup:

class Message
  belongs_to :recipient, polymorphic: true
end

class Membership
end

Message has already had the recipient_id and recipient_type on the table, which obviously previously to this upgrade was working correctly and calling message.recipient worked appropriately, but this is now where it's failing.

These tests I'm running are in, let's say App1, which is using the Factory gem of ours and the Models gem as well. This particular spec is a job spec, that runs through a job, but then of course ends up in the Message model in the Models gem, trying to do some work on the message, and calls message.recipient and gets recipient as nil. Reload does not work.

I've tried:

  • Reloaded the message at many different points in the job process, but didn't work.
  • Rewriting the specs (ie context changes, let vs let! changes), but that did not fix it.
  • I ran the same types of test on the model Message itself in the Models gem specs, and it works fine.

To me, it seems as though this is an issue of communication between the gems and the app itself and the loading of the polymorphic associations for each individual spec, but despite going through all the configs and trying to make some changes I have not been able to pinpoint the issue, although it's obviously possible that I missed something here. I had begun to think this was purely an issue in our Factory gem, but the message instance not being created via the Factory, although in an entirely different spec that is exhibiting the same behavior, the object that is failing is being created in the Factory. So because of that, it seems the issue is simply between the polymorphic association being loaded correctly before each spec.

ETA: We are using Spork, and we have this in spec_helper.rb, although this is existing code and not new to the upgrade

Spork.each_run do
  # This code will be run each time you run your specs.
  FactoryBot.reload
  ActiveSupport::Dependencies.clear
  Dir[Rails.root + 'app/jobs/*.rb'].map {|f| File.basename(f, '.*').camelize.constantize }
end

Rails 4.2

rspec-rails 3.9.1

Ruby 2.3.6

0

There are 0 best solutions below