Giter VIP home page Giter VIP logo

wisper-rspec-compat's Introduction

Wisper::Rspec

Rspec matcher and stubbing for Wisper.

Gem Version Build Status

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).

# with arguments matcher
expect { publisher.execute }.to broadcast(:event, hash_including(a: 2))

Rspec values matcher can be used to match arguments. This assertion matches broadcast(:another_event, a: 2, b: 1) but not broadcast(:another_event, a: 3)

Matchers can be composed using compound rspec matchers:

expect {
  publisher.execute(123)
  publisher.execute(234)
}.to broadcast(:event, 123).and broadcast(:event, 234)

expect {
  publisher.execute(123)
  publisher.execute(234)
}.to broadcast(:event, 123).or broadcast(:event, 234)

Note that the broadcast method is aliased as publish, similar to the Wisper library itself.

Not broadcast matcher

If you want to assert a broadcast was not made you can use not_broadcast which is especially useful when chaining expectations.

expect {
  publisher.execute(123)
}.to not_broadcast(:event, 99).and broadcast(:event, 123)

Using message expectations

If you need to assert on the listener receiving broadcast arguments you can subscribe a double with a message expectation 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.

wisper-rspec-compat's People

Contributors

krisleech avatar shkm avatar merrington avatar gav1n avatar nikolai-b avatar jbmeerkat avatar theteanerd avatar ecbrodie avatar volodymyr-mykhailyk avatar adis-io avatar gjselten avatar stevenewson avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.