ruby-on-rails - 带有 rspec 和 factory_girl_rails 的 Rails 4 安装引擎

标签 ruby-on-rails ruby-on-rails-4 rspec factory-bot rspec-rails

我正在使用 mongoid odm 构建一个安装在 Rails 4 上的应用程序。一切正常,但 rspec 测试无法正常工作。当我运行 bundle exec rspec 时出现错误:

Factory not registered: cafcaf_user

我的用户模型:

module Cafcaf
  class User
    include Mongoid::Document
    field :username, type: String
    field :email, type: String
    field :full_name, type: String
    field :last_name, type: String
  end
end

我的 spec_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../spec/test_app/config/environment.rb", __FILE__)
require 'rspec/rails'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = "random"
end

我的 user_spec.rb

require 'spec_helper'
module Cafcaf
  describe User do

    it "has a valid factory" do 
        FactoryGirl.create(:cafcaf_user).should be_valid
    end
    it "is invalid without a username"
    it "is invalid without an email"
  end
end

我的工厂.rb

FactoryGirl.define do
  factory :cafcaf_user, :class => 'User' do
    username "MyString"
    email "MyString"
    full_name ""
    last_name "MyString"
  end
end

我的库/cafcaf/engine.rb

module Cafcaf
  class Engine < ::Rails::Engine
    isolate_namespace Cafcaf
    config.generators do |g|
      g.test_framework :rspec
      g.fixture_replacement :factory_girl, :dir => 'spec/factories'
    end
  end
end

我的 gem 文件

source "https://rubygems.org"


gem 'rails', "~> 4.0.4"
gem 'mongoid', github: 'mongoid/mongoid', tag: 'v4.0.0.beta1'

group :development, :test do 
  gem 'rspec-rails', '~> 3.0.0.beta2'
  gem 'database_cleaner', '~> 1.2.0'
  gem 'factory_girl_rails', '~> 4.4.1'  
end

我的 gem 规范

$:.push File.expand_path("../lib", __FILE__)
require "cafcaf/version"

# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
  s.name        = "cafcaf"
  s.version     = Cafcaf::VERSION
  s.authors     = ["Your name"]
  s.email       = ["bla@bla.com"]
  s.homepage    = "http://ir.io"
  s.summary     = "Summary of Cafcaf."
  s.description = " Description of Cafcaf."

  s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]

  s.add_dependency "rails", "~> 4.0.4"
end

我不知道如何继续。如何在已安装的 Rails 引擎应用程序中使用 rspec 和 factory_girs gem?我做了很多测试,但没有找到解决方案。

最佳答案

我通过将以下代码添加到 spec_helper.rb 找到了解决方案

 ENGINE_RAILS_ROOT=File.join(File.dirname(__FILE__), '../')
 Dir[File.join(ENGINE_RAILS_ROOT, "spec/factories/**/*.rb")].each {|f| require f }

现在它就像一个魅力。

关于ruby-on-rails - 带有 rspec 和 factory_girl_rails 的 Rails 4 安装引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22860025/

相关文章:

ruby-on-rails - rails + Capistrano `rbenv: 2.3.1 is not installed or not found in $HOME/.rbenv/versions/2.3.1`

ruby-on-rails - 如何使用 Rspec 测试后台作业 (Sidekiq)?

ruby-on-rails - rspec-core 中的 find_failed_line 错误

ruby-on-rails - 使用 STI 时是否可以从包含关联中提取?

ruby-on-rails - 工厂机器人 : create the same object multiple times

ruby - 如何在 Ruby 中使用 RSpec 模拟 Pusher?

ruby-on-rails - 在 Rails session 中保存数据以在下一次请求时以表单形式使用

ruby-on-rails - Rails 有许多动态条件子句

html - 使用类名称中带有空格的 CSS 更改类

ruby-on-rails - rspec :type? 的有效值是什么