他のプラグイン (プラグインと宝石、同じもの) のテスト スイートを見てみましょう。
Active Record を拡張するプラグインのテストは非常に簡単です。Active Record はどこでも使用できるため、その場合は、Active Record をデータベース (インメモリ sqlite データベースなど) に接続し、コードをテストするだけです。
# test/test_helper.rb
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
require 'your_thing'
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
ActiveRecord::Schema.define(:version => 1) do
create_table :posts do |t|
t.string :title
t.text :excerpt, :body
end
end
class Post < ActiveRecord::Base
validates_presence_of :title
end
# test/my_test.rb
require 'test_helper'
class MyTest < Test::Unit::TestCase
def test_thing
# use Post here
end
end
コントローラーのアクションなどをテストしたい場合は難しくなります。私のライブ バリデーション プラグインには、多くのことを模擬して ERb ビューを文字列として渡すことができる、非常に広範なテスト スイートがあります。
これらの Test::Unit の例を RSpec に適応させることは、Test::Unit の何にも便乗しないので簡単です。