Skip to content

TDD in Hyrax

Adam Ploshay edited this page Dec 6, 2018 · 3 revisions

Scratch page to collect useful info about RSpec testing in Hyrax and specifically in ESSI...

Factories

As of PR #14, we reference FactoryBot factories from the Hyrax gem. The benefit of doing so is that we can access Hyrax's full set of factories that will usually be exactly what is needed to setup a test example. The alleviates literally copying them down and then trying to keep them in sync through version upgrades.

This is done by adding the gem's spec directory on disk to the locations that are searched for factories to initialize. This has to be done before RSpec/FactoryBot initializes so the line that adds the new location is located in the main Hyrax initializer.

There are several implications to doing this. First, it is not going to be obvious that there are named factories referenced elsewhere since this isn't an overly familiar pattern and this will be compounded by the fact that the code that sets it up won't be in any of the spec helpers so it won't be obvious. Secondly, any time the behavior of a factory needs to be tweaked for local use cases, the modify method must be used instead of define or tests will fail due to duplicate name error. Being able to extend/override an existing factory is good and very flexible but the problem is that you may not know that factories are being referenced elsewhere (see point # 1) and the resulting duplicate name error would be perplexing.

Factories can be overridden like:

FactoryBot.modify do
   factory :user do  # This named factory must be defined elsewhere first
     full_name     "Jane Doe"
     custom_attr  "Something"
   end
end

For an example, see the User factory that has been redefined in ESSI.