Rubyでのjasmineの利用
jasmine gemは、Railsの有無にかかわらず使用できます。(注:jasmine gemはAsset Pipelineのみをサポートし、Webpackerはサポートしていません。jasmine-browser-runner は、RailsのJavaScript管理のどちらのスタイルでも動作します。)
Gemfileにjasmine gemを追加し、bundle install
を実行してください。
gem 'jasmine'
セットアップ
デフォルトのjasmine.ymlとサンプルのjasmine_helper.rbをインストールするには、プロジェクトでRailsを使用している場合、jasmineにはセットアップに使用できるジェネレーターがあります。
rails g jasmine:install
プロジェクトでRailsを使用していない場合、これらのすべてのコマンドはコマンドラインツール jasmine
でも使用できます。コマンドラインツールは、jasmineタスクを読み込むようにRakefileも変更します。
jasmine init
Jasmineには、実装を含む、インストールできるサンプルスペックもいくつかあります。
rails g jasmine:examples
jasmine examples
使用方法
jasmine.yml
をインストールすると、rake
で2つのコマンドが使用できるようになります。
ブラウザでアクセスできるように、継続して実行されるサーバーを起動する場合
rake jasmine
CIサーバーで使用する場合
rake jasmine:ci
デバッグのために、jasmine.yml
のランダム化設定を個々の `jasmine:ci` 実行でオーバーライドすることもできます。
rake jasmine:ci[true]
シードを指定することもできます。
rake jasmine:ci[true,4321]
これで、最初のスイートを作成できるはずです。
設定
主な設定は、デフォルトで spec/javascripts/support
にある jasmine.yml
で行います。 jasmine.yml
には、テストを実行するページに読み込むファイルと、より複雑な設定のために読み込むRubyファイルの場所が設定されています(以下に示します)。
jasmine.yml
の spec_helper
キーで指定された jasmine_helper.rb
ファイルは、configureブロックで構成されています。
Jasmine.configure do |config|
# You can add rack handlers for specific urls
config.add_rack_path '/something' do
[200]
end
# And mount other rack applications
config.add_rack_app MyRackApp
# You can configure the port that the `rake jasmine` command starts a server on
config.server_port = 12345
# You can configure the port that the `rake jasmine:ci` command starts it's server on
config.ci_port = 54321
# You can add [custom formatters](#section-Custom_Formatters)
config.formatters << My::Custom::Formatter
# You can use a [custom runner](#section-Custom_Runners)
# The `runner` option on config should be a lambda or Proc that receives a formatter
# and server url and returns a constructed runner object. The lambda allows you to
# configure other options that need to be configured at initialization time.
config.runner = lambda do |formatter, server_url|
My::Custom::Runner.new(formatter, server_url, 100)
end
end
デフォルトのphantomjsランナーの設定
phantomjsランナーは、いくつかの追加オプションをサポートしています。
スペックからの console.log
メッセージの出力を表示する場合は、show_console_log を true に設定します。
show_console_log: true
phantomjsウェブページオブジェクトを設定する必要がある場合は、設定スクリプトを指定できます。
phantom_config_script: 'relative/path/from/project/root.js'
このファイルは、phantomランナーによって require
され、configure
関数には、構築された page
オブジェクトが渡されます。
exports.configure = function(page) {
page.viewportSize = {
width: 340,
height: 220
};
};
カスタムフォーマッター
デフォルトでは、jasmine:ci
rakeタスクは、スペックの実行中に.
、F
、*
を出力し、最後にサマリーを出力します。出力を変更したり、他の出力を生成したりする場合は、カスタムフォーマッターが必要になります。カスタムフォーマッターを追加する方法の例については、設定セクションを参照してください。
カスタムフォーマッターは、format
と done
の2つのメソッドを実装する必要があります。
class My::Custom::Formatter
# `format` is called by the runner every time it gets a batch of results from the page.
# The parameter will be an array of `Jasmine::Result` objects
def format(results)
results.each do |result|
puts result.status
end
end
# `done` will be called by the runner after all results have come in.
def done
puts 'Done running tests'
end
end
jasmineチームは、解析方法を知っているCIサーバーで使用するために、junitスタイルのXMLを生成するカスタムフォーマッターも保守しています。 Jasmine JUnit XML Formatter
カスタムランナー
デフォルトでは、jasmine:ci
rakeタスクはphantomjsを使用してjasmineスペックランナーページを読み込み、テストを実行します。別のブラウザでテストを実行したり、phantomの使用方法を変更したりする場合は、カスタムランナーが必要になります。カスタムランナーを追加する方法の例については、設定セクションを参照してください。
構築されたランナーは、run
メソッドを実装するだけで済みます。
class My::Custom::Runner
def initialize(formatter, jasmine_server_url, result_batch_size)
# The formatter passed in is responsible for making sure all configured
# formatters receive the same messages.
@formatter = formatter
# The `jasmine_server_url` is the full http://<host>:<port> url where
# the jasmine server was started
@jasmine_server_url = jasmine_server_url
@result_batch_size = result_batch_size
end
# `run` is responsible coordinating the test run.
def run
# Here we're using Phantom to load the page and run the specs
command = "#{Phantomjs.path} 'phantom_run.js' #{@jasmine_server_url} #{@result_batch_size}"
IO.popen(command) do |output|
# The `phantom_jasmine_run.js` script writes out batches of results as JSON
output.each do |line|
raw_results = JSON.parse(line, :max_nesting => false)
# Formatters expect to get `Jasmine::Result` objects.
# It is the runner's job to convert the result objects from the page,
# and pass them to the `format` method of their formatter.
results = raw_results.map { |r| Result.new(r) }
@formatter.format(results)
end
end
# When the tests have finished, call `done` on the formatter to run any
# necessary completion logic.
@formatter.done
end
# If the runner needs some javascript to be loaded into the page as part of the load,
# it returns the full path in `boot_js`
def boot_js
File.expand_path('runner_boot.js', __FILE__)
end
end
jasmineチームは、selenium(およびオプションでSauceLabs)を使用して他のブラウザでスペックを実行するカスタムランナーも保守しています。 Jasmine Selenium Runner