File: README — activeldap-fabrication - ActiveLdap

ActiveLdap Fabrication

FabricationActiveLdap アダプターです。

説明

‘ActiveLdap Fabrication’はFabricationのActiveLdapアダプターです。これを使うとActiveLdapの代替フィクスチャーシステムとしてFabricationを使えるようになります。

必要なもの

インストール

Here are install steps for Rails 3:

  • Add activeldap-fabrication gem to your Gemfile.
  • Change fixture_replacement to Fabrication in your config/application.rb.
  • Add require "active_ldap_fabrication" in your test/test_helper.rb or spec/spec_helper.rb.

Gemfile:


group :development, :test do
  # ...
  gem "activeldap-fabrication" # <- Add this.
end

config/application.rb:


config.generators do |g|
  # For test-unit
  g.test_framework :test_unit, :fixture => true, :fixture_replacement => "fabrication"
  g.fixture_replacement :fabrication, :dir => "test/fabricators"
  # For RSpec
  # g.test_framework      :rspec, :fixture => true, :fixture_replacement => "fabrication"
  # g.fixture_replacement :fabrication
end

test/test_helper.rb:


ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require "active_ldap_fabrication" # <- Add this.

class ActiveSupport::TestCase
  # Add more helper methods to be used by all tests here...

  # LDAP: start
  setup do
    @dumped_data = nil
    begin
      @dumped_data = ActiveLdap::Base.dump(:scope => :sub)
    rescue ActiveLdap::ConnectionError
    end
    ActiveLdap::Base.delete_all(nil, :scope => :sub)
    populate_ldap
  end

  teardown do
    if @dumped_data
      ActiveLdap::Base.setup_connection
      ActiveLdap::Base.delete_all(nil, :scope => :sub)
      ActiveLdap::Base.load(@dumped_data)
    end
  end

  def populate_ldap
    populate_ldap_base
    populate_ldap_ou
  end

  def populate_ldap_base
    ActiveLdap::Populate.ensure_base
  end

  def populate_ldap_ou
  end
  # LDAP: end
end

使い方

Userモデルを生成します:

% script/rails generate model User --classes person

test/test_helper.b内でou=Usersを作成します。


# ...
class ActiveSupport::TestCase
  # ...
  def populate_ldap_ou
    ActiveLdap::Populate.ensure_ou("Users") # <- Add this.
  end
end

test/fabricators/user_fabricator.rbでテストデータを定義します:


Fabricator(:user) do
end

Fabricator(:bob, :from => :user) do
  cn "Bob"
  sn "Dyran"
end

test/unit/user_test.rbでUserモデルのテストを書きます:


require 'test_helper'

class UserTest < ActiveSupport::TestCase
   test "cn" do
     assert_equal("Bob", Fabricate(:bob).cn)
   end
end

We run tests:

% bundle exec rake test
Loaded suite /var/lib/gems/1.9.1/gems/rake-0.9.2/lib/rake/rake_test_loader
Started

UserTest:
     PASS cn (1.10s)

Finished in 1.101005 seconds.

1 tests, 1 assertions, 0 failures, 0 errors, 0 skips

作者

  • Copyright © 2011 Kouhei Sutou <kou@clear-code.com>

ライセンス

This program is free software; you can redistribute it and/or modify it. It is dual licensed under Ruby’s license and under the terms of the Lesser GNU General Public License as published by the Free Software Foundation; either version 2.1, or (at your option) any later version.

ライセンス条項はCOPYINGファイルを見てください。

感謝