Rails Engines: How to write tests that depend on the underlying application models?
The Rails Engines documentation was very good for understanding how to write engines. However, this left me with a question that I could not answer.
Let's say I am writing an engine discussed in the documentation (blog). The model Post
doesn't need to know anything about its author, and this engine doesn't need to deal with accounts anyway, but it makes sense for posts to have an author, so we make a class for the author and we say Post
model belongs_to
it. In the main application, the engine simply uses any account scheme.
How, then, do we write tests for a model Post
if the engine does not contain a model for the author? I am using FactoryGirl and I tried to just link the factory for the user, but it doesn't work without the appropriate table. I suppose I could create a base class Author
in the generated application test/dummy
, but then all tests had to be added there as well, which seems a little annoying to me (I just want my tests to be obvious where I think).
source to share
The answer is actually pretty simple: use an app test/dummy
. Create an Author class inside test/dummy
, but write a factory for it test/factories
and use it like everything else. You don't need to write any tests in a dummy application. More information (including a quick tutorial) is here:
https://rainveiltech.com/posts/rails-writing-engine-tests-that-depend-on-main-application-models
source to share