Question

I am building a rails 4 mounted application with mongoid odm. Everything works fine but rspec tests does not work properly. When I run bundle exec rspec an error occurs saying that:

Factory not registered: cafcaf_user

My user model:

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

my 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

my 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

my factories.rb

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

my lib/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

my Gemfile

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

my gemspec

$:.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

Exactly I do not know ho to proceed. How to use rspec and factory_girs gem in a mounted rails engine app? I have done tons of test but did not find the solution.

Was it helpful?

Solution

I have found the solution by adding the following codes to spec_helper.rb

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

Now it works like a charm.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top