ファイル: はじめに — ActiveLdap Fabrication - ActiveLdap

ActiveLdap Fabrication

FabricationActiveLdapアダプタ。

説明

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

必要なもの

インストール

Rails 3用のインストール手順:

  • Gemfileにactiveldap-fabrication gemを追加します。
  • config/application.rbでfixture_replacementをFabricationに変更します。
  • test/test_helper.rbまたはspec/spec_helper.rbにrequire "active_ldap_fabrication"を追加します。

Gemfile:

group :development, :test do
  # ...
  gem "activeldap-fabrication" # <- これを追加。
end

config/application.rb:

config.generators do |g|
  # test-unit用
  g.test_framework :test_unit, :fixture => true, :fixture_replacement => "fabrication"
  g.fixture_replacement :fabrication, :dir => "test/fabricators"
  # 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" # <- これを追加。

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

  # LDAP: ここから
  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

使い方

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") # <- これを追加。
  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ファイルを見てください。

感謝