(I have found a few similar questions on stackoverflow and beyond that all seem to point to adding those lines in engine.rb, or specifying namespaces in the factories.rb but I am not using namespaces with this engine.)推荐答案我发现最简单的方法是添加一个安装生成器,该生成器只是将工厂复制过来.我还让生成器运行install migrations rake任务,因为在使用该引擎的任何应用中都需要这些.I found the easiest route to take with this was to add an install generator that simply copies the factories over. I also have the generator run the install migrations rake task as I will need these in any apps that use the engine.因此,在lib/generators/my_engine/install/install_generator.rb中:module MyEngine module Generators class InstallGenerator < Rails::Generators::Base source_root File.expand_path('../templates', __FILE__) def copy_migrations rake("my_engine:install:migrations") end def copy_factories copy_file "../path/to/spec/factories.rb", "spec/factories.rb" end end endend现在在使用该引擎的项目中,我只需运行rails generate my_engine:install,工厂(和迁移)就可以使用了.Now in projects that use this engine, I simply run rails generate my_engine:install and the factories (and migrations) are ready for me to use. 这篇关于Engine的Rails 4 Use Factory Girl工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-23 20:32