No commit activity in last 3 years
No release in over 3 years
active_resource_test_helper makes it easier to use ActiveResouce::HttpMock. Instead of declaring manually all the request-response pairs, it’s possible to use dynamically generated contents. These contents are defined using factory_girl and are stored into a Redis database using ohm.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 1.2.3
>= 0.0.35
>= 1.0.4
 Project Readme

active_resource_test_helper¶ ↑

active_resource_test_helper makes it easier to use ActiveResouce::HttpMock.

Instead of declaring manually all the request-response pairs, it’s possible to use dynamically generated contents. These contents are defined using factory_girl and are stored into a Redis database using ohm.

Usually ActiveResource tests look like that:

def setup
  @matz  = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
  ActiveResource::HttpMock.respond_to do |mock|
    mock.post   "/people.xml",   {}, @matz, 201, "Location" => "/people/1.xml"
    mock.get    "/people/1.xml", {}, @matz
    mock.put    "/people/1.xml", {}, nil, 204
    mock.delete "/people/1.xml", {}, nil, 200
  end
end

def test_get_matz
  person = Person.find(1)
  assert_equal "Matz", person.name
end

While using active_resource_test_helper an common ActiveResource test would be something like that:

class RemoteUserTest < Test::Unit::TestCase
  include ActiveResourceTestHelper
  active_resource_factories :user

  def setup
    assert_equal 0, User.count
    @users = []
    20.times do |i|
      @users << Factory.create(:basic_user)
    end
  end

  def test_find_by_id
    @users.each do |expected_user|
      user = UserResource.find(expected_user.id)
      assert_not_nil user
      assert_equal expected_user.first_name, user.first_name
    end
  end
end

HTTP methods supported¶ ↑

Currently active_resource_test_helper dynamic contents are served only by get requests. In the future they will be used also by post, put, delete and head operations.

Get requests¶ ↑

The following operations are currently fully supported:

Person.find(1)
Person.find(:all)
Person.find(:first)
Person.find(:last)
Person.find(:all, :params => { :title => "CEO" })
Person.find(:first, :params => { :first_name => "flavio", :last_name => "castelli" })

Requirements¶ ↑

Install Redis. On most platforms it’s as easy as grabbing the sources, running make and then putting the redis-server binary in the PATH.

Once you have it installed, you can execute redis-server and it will run on localhost:6379 by default. Check the redis.conf file that comes with the sources if you want to change some settings.

Then install the active_resource_test_helper gem:

sudo gem install active_resource_test_helper

Usage¶ ↑

In order to use active_resource_test_helper inside of your tests you have to:

  • require ‘active_resource_test_helper’

  • include the ActiveResourceTestHelper module inside of your test

This is a small example:

require  'active_resource_test_helper'

class MyTest < Test::Unit::TestCase
  include ActiveResourceTestHelper
  active_resource_factories :user, :post, :comment
end

Obviously you have also to define the factories used by your test (see below).

You can find more examples under the test directory.

Defining factories¶ ↑

Factories used by ActiveResource::HttpMock are defined with the Factory.define_active_resource_factory method:

Factory.define_active_resource_factory(:basic_user, :class => "User") do |u|
  u.sequence(:first_name) {|n| "first_name#{n}"}
  u.sequence(:last_name) {|n| "last_name#{n}"}
  u.admin false
  u.email {|u| "#{u.first_name}.#{u.last_name}@example.com" }
  u.age {rand(30) + 18}
end

If you are already familiar with factory_girl you will probably have already noticed we are using the same syntax.

What happens behind the scenes¶ ↑

active_resource_test_helper will automatically generate an ohm model for each build class used by the active resource factories.

These models will have one attribute and one index per each attribute declared inside of the factory.

The factory defined into the previous example will generate this model:

class User < Ohm::Model
    index :id

    attribute :first_name
    index :first_name

    attribute :last_name
    index :last_name

    attribute :admin
    index :admin

    attribute :email
    index :email

    attribute :age
    index :age

    alias :save! :save
end

Transactional factories¶ ↑

Currently redis doesn’t have a complete transaction support. active_resource_test_helper removes from the redis database all the instances of the models defined by the ActiveResource factories that are used by the test. This operation is executed before and after each test execution.

Statically defined responses¶ ↑

It’s possible to use dynamic contents and static pairs of request-response at the same time. However the responses defined in the static way have precedence over those generated in the dynamic way.