Skip to content
Travis Douce edited this page Apr 9, 2014 · 74 revisions

Create factory_girl factories remotely using remote_factory_girl in conjuction with remote_factory_girl_home_rails. Overcoming the problem with integration testing SOA apps

Installation

remote_factory_girl should live in the client application and remote_factory_girl_home_rails should live in the home app (the app with factory_girl factories).

Client App

Add this line to the client application's Gemfile:

group :test do
  gem 'remote_factory_girl'
end

And then execute:

$ bundle

Or install it yourself as:

$ gem install remote_factory_girl

Configure

Basic Configuration

Configure in spec/spec_helper.rb

RemoteFactoryGirl.configure do |config|
  config.home = { host: 'localhost', port: 5000, end_point: '/remote_factory_girl' }
  config.return_with_root = false
  config.return_response_as = :dot_notation
end

Use in specs

require 'spec_helper'

describe User do
  it 'should create a user factory in RemoteFactoryGirlHome' do
    user = RemoteFactoryGirl.create(:user, first_name: 'Sam', last_name: 'Iam')
    expect(user.first_name).to eq('Sam')
  end
end

NOTE: user must be a factory defined in home application

ActiveResource Configuration

If your apps are configured to use ActiveResource, then have remote_factory_girl return ActiveResource objects.

Configure in spec/spec_helper.rb

RemoteFactoryGirl.configure do |config|
  config.home = { host: 'localhost', port: 5000, end_point: '/remote_factory_girl' }
  config.return_as_active_resource = true 
end

Use in specs

require 'spec_helper'

describe User do
  it 'should create a user factory in RemoteFactoryGirlHome' do
    user = RemoteFactoryGirl.create(:user_with_friends, first_name: 'Sam', last_name: 'Iam').resource(User)
    expect(user.first_name).to eq('Sam')
  end
end

NOTE: user_with_friends must be a factory defined in home application

Clean database between specs (recommended)

See remote_database_cleaner and remote_database_cleaner_home_rails for installation and configuration

Configure in spec/spec_helper.rb

RSpec.configure do |config|
  config.after(:before) do
    RemoteDatabaseCleaner.clean
  end
end

Home App

Add this line to home application's Gemfile:

group :test do
  gem 'remote_factory_girl_home_rails'
end

NOTE: factory_girl has to be included in the same group

And then execute:

$ bundle

Configuration

Configure in config/environments/*.rb

Activate remote_factory_girl_home_rails to run in the environments in which it is intended to run. For example, if remote_factory_girl_home_rails is included in group :test (most common), then activate it in config/environments/test.rb

YourApplication::Application.configure do
  ...
  config.remote_factory_girl_home_rails.enable = true
  ...
end

Configure in config/routes.rb

YourApplication::Application.routes.draw do
  if defined?(RemoteFactoryGirlHomeRails::Engine)
    mount RemoteFactoryGirlHomeRails::Engine, at: '/remote_factory_girl' 
  end
end

Configure in config/initializers/remote_factory_girl_home_rails.rb

Specify any methods that should be skipped for incoming http requests. The most common methods to skip are authentication related methods that live in ApplicationController.

RemoteFactoryGirlHomeRails.configure do |config|
  config.skip_before_filter = [:authenticate, :some_other_method]
end if defined?(RemoteFactoryGirlHomeRails)

Use Together

Home Application

  1. Run any outstanding migrations.
  2. Start the home application's server with the correct:
    • environment
    • port
    • end_point

Given the configuration from the examples above, start the home server with:

$ rails server --environment=test --pid=/Users/your_app/tmp/pids/your_app-test.pid --port=5000

Client Application

  1. Run your test suite.
$ rspec
Clone this wiki locally