Note: I’m only posting this question so that others may find it if they ever need to, as I have found a good solution.
In my controller tests, I don’t want to commit to the database, but I still want the controllers to use finder methods to get mock objects (by mocking the find method), but then I want to use dom_id on those mocks in assert_select, to verify that they’re being displayed.
However, since they’re non-saved objects, dom_id keeps returning new_object instead of object_1, object_2, et cetera.
Is there any quick way to get it to work? I really don’t want to persist real records in tests.
Suppose you’re using
ActiveSupport::TestCaseandFactoryGirlfor building model objects, you can add this to yourtest/test_helper.rbfile:Then, suppose you’re using
RRas your mocking library, you can do something like this: