opal-rspec
allows opal to use rspec for running specs in javascript
environments. It comes with built-in support for running rspec with
standard web browser formatters. Also, async spec examples
are supported to reflect browser usage of ruby applications.
describe User do
it "can be created with a name" do
expect(User.new).to_not be_persisted
end
end
Add the opal-rspec
gem to your Gemfile:
# Gemfile
gem 'opal'
gem 'opal-rspec'
opal-rspec
can use sprockets to build and serve specs over a simple rack server. Add the following to a config.ru
file:
# config.ru
require 'bundler'
Bundler.require
sprockets_env = Opal::RSpec::SprocketsEnvironment.new
run Opal::Server.new(sprockets: sprockets_env) { |s|
s.main = 'opal/rspec/sprockets_runner'
s.append_path 'spec'
s.debug = false
}
Then run the rack server bundle exec rackup and visit http://localhost:9292
in any web browser.
opal-rspec
adds support for async specs to rspec. These specs are defined using
#async
instead of #it
:
describe MyClass do
# normal example
it 'does something' do
expect(:foo).to eq(:foo)
end
# async example
async 'does something else, too' do
# ...
end
end
This just marks the example as running async. To actually handle the async result,
you also need to use a run_async
call inside some future handler:
async 'HTTP requests should work' do
HTTP.get('/users/1.json') do |res|
run_async {
expect(res).to be_ok
}
end
end
The block passed to run_async
informs the runner that this spec is finished
so it can move on. Any failures/expectations run inside this block will be run
in the context of the example.