今まで通りだと動きません。test/test_helper.rbに下記を追加。

require 'shoulda/rails'

今βのshoulda 3.xなら大丈夫なのでもうすぐこれも必要無くなるみたいです。

module Foo
  module Helpers
    def bar
      'unk'
    end
  end
end

こういうHelpersを

helpers do
  include Foo::Helpers
end

こういう風に使ってた場合。

RSpec.configure do |config|
  config.include Foo::Helpers
end
describe Foo::Helpers do
  context 'bar' do
    it 'should return unk' do
      bar.should eql('unk')
    end
  end
end

Spec::Runner.configでincludeするとテスト出来る。

LokkaはSinatraベースなので同じようにHelpersのテスト書ける。でもRspecややこしいな。config.includeのとことか。とTest::Unit, Shoulda信者が申しております。

Test::Unit + Capybaraによる自動テスト。

Test::UnitはRailsデフォルトだけあってサポートが行き届いていて簡単。

JenkinsでRailsアプリをテストする。

環境

Jenkinsのインストール

さくらのVPSにDebian squeezeをインストールする方法はこちら

$ wget -q -O - http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
$ sudo vi /etc/apt/sources.list
deb http://pkg.jenkins-ci.org/debian binary
$ sudo apt-get update
$ sudo apt-get install jenkins

jenkinsユーザーが作成されて8080にjenkinsが立ち上がる。

nginxでのReverse proxyの設定

example.com:8080では味気無いのでci.example.comでアクセス出来るようにする。

$ sudo apt-get install nginx
$ sudo vi /etc/nginx/sites-available/ci.example.com
server {
  listen 80;
  server_name ci.example.com;
  location / { proxy_pass http://localhost:8080; }
}
$ ln -s /etc/nginx/sites-available/ci.example.com /etc/nginx/sites-enable/ci.example.com

rvmのインストール

rvmをsystem wideにインストールするのは大変なので単にjenkinsユーザーにインストールする。

$ sudo su - jenkins
$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

大体nokogiriを使う段になって困るので下記をちゃんとインストールしておく。

$ rvm package install readline openssl zlib
$ rvm install 1.8.7 -C --with-openssl-dir=$HOME/.rvm/usr --with-readline-dir=$HOME/.rvm/usr --with-zlib-dir=$HOME/.rvm/usr

アプリ名でgemsetを作っておき、bundlerをインストールしておく。(jenkinsのタスクでやっても構わないと思う)

$ rvm use ruby-1.8.7-p334
$ rvm gemset create rails-example-app
$ rvm use ruby-1.8.7-p334@rails-example-app
$ gem install bundler

ユーザー認証

LAN内でない場合はユーザー認証をかける。"Manage Jenkins"の"Access Control"で"Secure Realm"を"Jenkins's own user database"にして"Allow users to sign up"にチェックを入れる。"Authorization"は"Matrix-based security"を選び、管理者ユーザーとなる予定のユーザー名を追加しておく。少し分かりづらいがここで設定した後、同じ名前でsign upをすれば良い。(間違って管理画面にはいれなくなってしまった場合はここを参考にしてリセットする。)

Configure System [Jenkins]

gitプラグインのインストール

大体が社内のgitやgithubから持ってくることになるのでgitプラグインをインストールする。"Manage Jenkins" > "Plugin Manager" > "Available"から"Git Plugin"を選択してインストールする。"Github Plugin"もついでに入れておくと良いかもしれない。

ビルドの設定

"Source Code Management"から"Git"を選択し、"Repositories" > "URL of repository"にリポジトリのURLを入れる。(githubのプライベートリポジトリを使っている場合はjenkinsユーザーで鍵を作り、githubの"Admin" > "Deploy Keys"から登録しておけばいい。)ビルドトリガーは好きに設定する。右側の"?"に親切な説明があるので迷わないだろう。ビルドはrailsなので"Execute shell"を選択する。rvmにはrvm-shellという指定の環境でshellを実行するうってつけのコマンドがあるのでそれをshebangに設定すればいい。

u2plus Config [Jenkins]

"Post-build Actions"で"E-mail Notification"を設定しておく。Debianデフォルトのexim4はメールが飛ぶようになってないのでいっそのことpostfixを入れた方が楽。

$ sudo apt-get install postfix

関連:Debian Squeeze install to Sakura VPS - komagata

CUIコマンドのend to endテストとはこんな感じだろうか?

spec/css_selector_command_spec.rb at master from komagata/css_selector - GitHub

# spec/css_selector_command_spec.rb:
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe "css_selector" do
  it "get title" do
    result = `echo '<html><head><title>foo</title></head><body><h1>bar</h1></body></html>' | bin/css_selector title`
    result.should == "foo\n"
  end

  it "get h1" do
    result = `echo '<html><head><title>foo</title></head><body><h1>bar</h1></body></html>' | bin/css_selector h1`
    result.should == "bar\n"
  end
end

どっかに参考になるプロジェクトがないかしら。

Railsでguardを使う。(rspec)

# Gemfile:
group :test do
  gem 'rspec-rails'
  gem 'guard-rspec'

  # for Mac
  gem 'rb-fsevent'
  gem 'growl'
end
$ guard init rspec
$ guard start