Wisper::Rspec
Rspec matcher and stubbing for Wisper.
Installation
gem 'wisper-rspec', require: false
Usage
Broadcast Matcher
In spec_helper
require 'wisper/rspec/matchers'
RSpec::configure do |config|
config.include(Wisper::RSpec::BroadcastMatcher)
end
In your specs:
expect { publisher.execute }.to broadcast(:an_event)
This will match both broadcast(:an_event)
and broadcast(:an_event, :arg_1)
.
# with optional arguments
expect { publisher.execute }.to broadcast(:another_event, :arg_1, :arg_2)
With event arguments, it matches only if the event is broadcast with those arguments. This assertion matches broadcast(:another_event, :arg_1, :arg_2)
but not broadcast(:another_event)
.
Using message expections
If you need to assert on the listener receiving broadcast arguments you can subscribe a double with a message expection and then use any of the argument matchers.
listener = double('Listener')
expect(listener).to receive(:an_event).with(some_args)
publisher.subscribe(listener)
publisher.execute
Stubbing publishers
You can stub publishers and their events in unit (isolated) tests that only care about reacting to events.
Given this piece of code:
class MyController
def create
publisher = MyPublisher.new
publisher.on(:some_event) do |variable|
return "Hello with #{variable}!"
end
publisher.execute
end
end
You can test it like this:
require 'wisper/rspec/stub_wisper_publisher'
describe MyController do
context "on some_event" do
before do
stub_wisper_publisher("MyPublisher", :execute, :some_event, "foo")
end
it "renders" do
response = MyController.new.create
expect(response).to eq "Hello with foo!"
end
end
end
This is useful when testing Rails controllers in isolation from the business logic.
You can use any number of args to pass to the event:
stub_wisper_publisher("MyPublisher", :execute, :some_event, "foo1", "foo2", ...)
See spec/lib/rspec_extensions_spec.rb
for a runnable example.
Contributing
Yes, please.