ruby_gem.rb |
|
---|---|
Using jasmine with rubyThe jasmine gem can be use both with and without Rails. |
|
Add the jasmine gem to your gemfile and |
gem 'jasmine'
|
SetupTo install a default jasmine.yml and a sample jasmine_helper.rb |
|
If your project is using Rails, jasmine has generators you can use to get everything set up. |
rails g jasmine:install
|
If your project doesn't use Rails, all of these commands also exist in the command line tool |
jasmine init
|
Jasmine also has some example specs, with implementation, that you can install. |
rails g jasmine:examples
jasmine examples
|
UseOnce you've installed your |
|
If you want to start a server that continues to run so you can point a browser at it |
rake jasmine
|
For use on your CI server |
rake jasmine:ci
|
ConfigurationPrimary configuration is done in |
|
The |
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 |
config.server_port = 12345
|
You can configure the port that the |
config.ci_port = 54321
|
You can add custom formatters |
config.formatters << My::Custom::Formatter
|
You can use a custom runner
The |
config.runner = lambda { |formatter, server_url| My::Custom::Runner.new(formatter, server_url, 100) }
end
|
Configuring the default phantomjs runnerThe phantomjs runner supports some additional options. If you want to see the output from |
show_console_log: true
|
If you need to configure the phantomjs webpage object, you can specify a config script. |
phantom_config_script: 'relative/path/from/project/root.js'
|
This file will be |
exports.configure = function(page) {
page.viewportSize = {
width: 340,
height: 220
};
};
|
Custom FormattersBy default the |
|
Your custom formatter must implement 2 methods, |
class My::Custom::Formatter
|
|
def format(results)
results.each do |result|
puts result.status
end
end
|
|
def done
puts 'Done running tests'
end
end
|
The jasmine team also maintains a custom formatter that produces junit style XML for use on a CI server that knows how to parse it. Jasmine JUnit XML Formatter |
|
Custom RunnersBy default the |
|
Once constructed, a runner only needs to implement a |
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 = jasmine_server_url
@result_batch_size = result_batch_size
end
|
|
def run
|
Here we're using Phantom to load the page and run the specs |
command = "#{Phantomjs.path} 'phantom_jasmine_run.js' #{@jasmine_server_url} #{@result_batch_size}"
IO.popen(command) do |output|
|
The |
output.each do |line|
raw_results = JSON.parse(line, :max_nesting => false)
|
Formatters expect to get |
results = raw_results.map { |r| Result.new(r) }
@formatter.format(results)
end
end
|
When the tests have finished, call |
@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 |
def boot_js
File.expand_path('runner_boot.js', __FILE__)
end
end
|
The jasmine team also maintains a custom runner that uses selenium (and optionally SauceLabs) to run your specs with other browsers. Jasmine Selenium Runner |
|