Skip to content
Travis Douce edited this page Jul 28, 2014 · 74 revisions

Overcome the problem with integration testing SOA apps by creating test data with factory_girl remotely using remote_factory_girl in conjunction with remote_factory_girl_home_rails. Also, see remote_database_cleaner and remote_database_cleaner_home_rails.

Demo Applications

See Integration Testing Rails SOA for sample client and home applications configured with factory_girl, remote_factory_girl_home_rails, remote_database_cleaner, and remote_database_cleaner_home_rails.

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/home' }
  config.return_with_root = false
  config.return_response_as = :dot_notation
end

Use in specs

require 'spec_helper'

describe 'the sign-in process' do
  it "should be able to sign-in" do
    user = RemoteFactoryGirl.create(:user_adim, email: '[email protected]', first_name: 'Sam', last_name: 'Iam', password: 'onefishtwofish')

    visit root_path
    fill_in 'Email', with: user.email
    fill_in 'Password', with: user.password
    click_button 'Sign-in'

    expect(page).to have_content 'Welcome Sam Iam!'
  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/home' }
  config.return_as_active_resource = true 
end

Use in specs

require 'spec_helper'

describe 'the sign-in process' do
  it "should be able to sign-in" do
    user = RemoteFactoryGirl.create(:user_adim, email: '[email protected]', first_name: 'Sam', last_name: 'Iam', password: 'onefishtwofish').resource(User)

    visit root_path
    fill_in 'Email', with: user.email
    fill_in 'Password', with: user.password
    click_button 'Sign-in'

    expect(page).to have_content 'Welcome Sam Iam!'
   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.before(:each) 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 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