Engine的Rails 4 Use Factory Girl工厂 [英] Rails 4 Use Factory Girl factories from Engine
问题描述
我已经创建了Rails引擎(完整,无法安装),以为许多不同的Rails应用程序提供模型.我使用Factory Girl Rails对该引擎进行了测试,并且所有测试对于该引擎本身都运行良好.
I've created a rails engine (full, not mountable) to provide models to a number of different rails apps. I use Factory Girl Rails to test this engine and the tests all run fine for the engine itself.
我现在希望能够在包含此引擎的其他应用中使用这些工厂.
I now want to be able to use these factories in other apps that include this engine.
Gemspec的依赖关系如下:
The dependencies for the Gemspec look like this:
s.add_dependency "rails", "~> 4.0.3"
s.add_dependency "mysql2", "~> 0.3.15"
s.add_development_dependency "rspec-rails", "~> 3.0.0.beta"
s.add_development_dependency "factory_girl_rails", "~> 4.4.1"
s.add_development_dependency "shoulda-matchers", "~> 2.5.0"
我已经在/spec/factories.rb中定义了我的工厂:
And i have defined my factories in /spec/factories.rb:
factory :user do
...
end
要将factory.rb添加到factory girl中的定义路径中,我将以下内容添加到了/lib/engine_name/engine.rb文件中:
To add the factories.rb to the definition paths in factory girl, I added the following to my /lib/engine_name/engine.rb file:
class Engine < ::Rails::Engine
initializer "model_core.factories", :after => "factory_girl.set_factory_paths" do
FactoryGirl.definition_file_paths << File.expand_path('../../../spec/factories.rb', __FILE__) if defined?(FactoryGirl)
end
end
在我的rails应用程序中,我通过向Gemfile添加以下内容来包含引擎:
In my rails apps I include the engine by adding the following to the Gemfile:
gem 'engine_name', git: "<GIT_LOCATION>"
我还向该应用程序添加了factory_girl_rails(有什么方法可以从引擎中公开它吗?而不是也必须在应用程序Gemfile中指定它?).
I also add factory_girl_rails to the app (is there a way I can expose this from the engine? rather than having to specify it in the apps Gemfile too?).
并要求在spec_helper.rb中使用工厂女用护栏:
And require factory girl rails in spec_helper.rb:
require 'factory_girl_rails'
现在,当我编写如下所示的控制器测试时:
Now when I write, say, a controller test like the following:
it "saves the user to the database" do
expect{post :create, user: attributes_for(:user)}.to change{User.count}.by(1)
end
我收到错误:未注册工厂:用户"
我通过打开ruby控制台并运行FactoryGirl.definition_file_paths
仔细检查了工厂女孩定义文件的路径,并且可以在输出中从引擎中看到factory.rb:"/home/.../gems/engine-name-abc123/spec/factories.rb"
I've double checked the factory girl definition file paths by opening the ruby console and running FactoryGirl.definition_file_paths
and i can see the factories.rb from the engine in the output: "/home/ ... /gems/engine-name-abc123/spec/factories.rb"
要使这些工厂可用,我还有什么需要做的吗?
Is there anything else i need to do to make these factories available?
(我在stackoverflow上发现了一些类似的问题,除此之外,所有这些似乎都指向在engine.rb中添加这些行,或在factory.rb中指定名称空间,但我没有在此引擎中使用名称空间.)>
(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
end
end
现在在使用该引擎的项目中,我只需运行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工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!