wisper-rspec-compat 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f54802ba45a1ad17a8e580ffe568d573a34506954a71b44d259c8f455f708618
4
+ data.tar.gz: 9c23e3ea6404655a2160557331d769df189481fa8bce5fb5a8376cde62af5f32
5
+ SHA512:
6
+ metadata.gz: c2bd72d0ec67353db404b319ccf3d4f7aa659fa24e12fd78e5eead60e82f17a31c80ef1de98cf4b441ec5fab5b230a70384eca3718e02dc1d868787847e200a1
7
+ data.tar.gz: 5a27d430bf60a51f510ac9cb91bb3a19af8cfa383c5cb2f985eea0ab82709680d918c76877b09bc6555b88ca9ef24ec749894819eb261b11c1f3372fe3529319
data/CHANGELOG.md ADDED
@@ -0,0 +1,33 @@
1
+ Unreleased
2
+
3
+ * Fixed the error of the missing matcher description (https://github.com/krisleech/wisper-rspec/pull/32)
4
+ * Add not_publish matcher (https://github.com/krisleech/wisper-rspec/pull/35)
5
+
6
+ 1.1.0 (15/May/2018)
7
+
8
+ Authors: Nikolai (nikolai-b), David Kennedy (TheTeaNerd)
9
+
10
+ * Add not_broadcast matcher (https://github.com/krisleech/wisper-rspec/pull/30)
11
+ * Doc fixes
12
+
13
+ 1.0.1 (24/Feb/2018)
14
+
15
+ Authors: Kris Leech, Konstantin Rudy (k-ruby)
16
+
17
+ * fixes: undefined method `id` when using LoggerBroadcaster [#22]
18
+
19
+ 1.0.0 (16/Sept/2017)
20
+
21
+ Authors: Adis Osmonov (adisos)
22
+
23
+ * Matchers shows diff when expectation fails #18
24
+
25
+ 0.0.3 (7/Mar/2017)
26
+
27
+ 0.0.2 (8/Jan/2015)
28
+
29
+ * Added support for argument matching [#1] by Gavin Todes (GAV1N)
30
+
31
+ 0.0.1 (27/Oct/2014)
32
+
33
+ * Abstracted rspec from [Wisper](https://github.com/krisleech/wisper) gem.
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Kris Leech
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,143 @@
1
+ # Wisper::Rspec
2
+
3
+ Rspec matcher and stubbing for [Wisper](https://github.com/krisleech/wisper).
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/wisper-rspec.png)](http://badge.fury.io/rb/wisper-rspec)
6
+ [![Build Status](https://travis-ci.org/krisleech/wisper-rspec.png?branch=master)](https://travis-ci.org/krisleech/wisper-rspec)
7
+
8
+ ## Installation
9
+
10
+ ```ruby
11
+ gem 'wisper-rspec', require: false
12
+ ```
13
+
14
+ ## Usage
15
+
16
+ ### Broadcast Matcher
17
+
18
+ In `spec_helper`
19
+
20
+ ```ruby
21
+ require 'wisper/rspec/matchers'
22
+
23
+ RSpec::configure do |config|
24
+ config.include(Wisper::RSpec::BroadcastMatcher)
25
+ end
26
+ ```
27
+
28
+ In your specs:
29
+ ```ruby
30
+ expect { publisher.execute }.to broadcast(:an_event)
31
+ ```
32
+ This will match both `broadcast(:an_event)` and `broadcast(:an_event, :arg_1)`.
33
+
34
+
35
+ ```ruby
36
+ # with optional arguments
37
+ expect { publisher.execute }.to broadcast(:another_event, :arg_1, :arg_2)
38
+ ```
39
+
40
+ 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)`.
41
+
42
+ ```ruby
43
+ # with arguments matcher
44
+ expect { publisher.execute }.to broadcast(:event, hash_including(a: 2))
45
+ ```
46
+
47
+ 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)`
48
+
49
+ Matchers can be composed using [compound rspec matchers](http://www.rubydoc.info/gems/rspec-expectations/RSpec/Matchers/Composable):
50
+
51
+ ```ruby
52
+ expect {
53
+ publisher.execute(123)
54
+ publisher.execute(234)
55
+ }.to broadcast(:event, 123).and broadcast(:event, 234)
56
+
57
+ expect {
58
+ publisher.execute(123)
59
+ publisher.execute(234)
60
+ }.to broadcast(:event, 123).or broadcast(:event, 234)
61
+ ```
62
+
63
+ Note that the `broadcast` method is aliased as `publish`, similar to the *Wisper* library itself.
64
+
65
+ ### Not broadcast matcher
66
+
67
+ If you want to assert a broadcast was not made you can use `not_broadcast` which is especially useful when chaining expectations.
68
+
69
+ ```ruby
70
+ expect {
71
+ publisher.execute(123)
72
+ }.to not_broadcast(:event, 99).and broadcast(:event, 123)
73
+ ```
74
+
75
+ ### Using message expectations
76
+
77
+ If you need to assert on the listener receiving broadcast arguments you can subscribe a double
78
+ with a [message expectation](https://github.com/rspec/rspec-mocks#message-expectations)
79
+ and then use any of the [argument matchers](https://github.com/rspec/rspec-mocks#argument-matchers).
80
+
81
+ ```ruby
82
+ listener = double('Listener')
83
+
84
+ expect(listener).to receive(:an_event).with(some_args)
85
+
86
+ publisher.subscribe(listener)
87
+
88
+ publisher.execute
89
+ ```
90
+
91
+ ### Stubbing publishers
92
+
93
+ You can stub publishers and their events in unit (isolated) tests that only care about reacting to events.
94
+
95
+ Given this piece of code:
96
+
97
+ ```ruby
98
+ class MyController
99
+ def create
100
+ publisher = MyPublisher.new
101
+
102
+ publisher.on(:some_event) do |variable|
103
+ return "Hello with #{variable}!"
104
+ end
105
+
106
+ publisher.execute
107
+ end
108
+ end
109
+ ```
110
+
111
+ You can test it like this:
112
+
113
+ ```ruby
114
+ require 'wisper/rspec/stub_wisper_publisher'
115
+
116
+ describe MyController do
117
+ context "on some_event" do
118
+ before do
119
+ stub_wisper_publisher("MyPublisher", :execute, :some_event, "foo")
120
+ end
121
+
122
+ it "renders" do
123
+ response = MyController.new.create
124
+ expect(response).to eq "Hello with foo!"
125
+ end
126
+ end
127
+ end
128
+ ```
129
+
130
+ This is useful when testing Rails controllers in isolation from the business logic.
131
+
132
+ You can use any number of args to pass to the event:
133
+
134
+ ```ruby
135
+ stub_wisper_publisher("MyPublisher", :execute, :some_event, "foo1", "foo2", ...)
136
+ ```
137
+
138
+ See `spec/lib/rspec_extensions_spec.rb` for a runnable example.
139
+
140
+
141
+ ## Contributing
142
+
143
+ Yes, please.
data/bin/console ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "wisper/rspec"
5
+ require 'wisper/rspec/matchers'
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,140 @@
1
+ require 'wisper'
2
+ require 'rspec/expectations'
3
+ require 'rspec/mocks/argument_list_matcher'
4
+
5
+ module Wisper
6
+ module RSpec
7
+ class EventRecorder
8
+ include ::RSpec::Mocks::ArgumentMatchers
9
+ attr_reader :broadcast_events
10
+
11
+ def initialize
12
+ @broadcast_events = []
13
+ end
14
+
15
+ def respond_to?(method_name)
16
+ true
17
+ end
18
+
19
+ def respond_to_missing?(*)
20
+ true
21
+ end
22
+
23
+ def method_missing(method_name, *args, **kwargs, &block)
24
+ @broadcast_events << [method_name.to_s, *args, **kwargs]
25
+ end
26
+
27
+ def broadcast?(event_name, *args, **kwargs)
28
+
29
+ expected_args = args.size > 0 ? args : [any_args]
30
+ expected_kwargs = kwargs.empty? ? {} : kwargs
31
+ @broadcast_events.any? do |event_params|
32
+ name, *args, kwargs = event_params
33
+ matcher = ::RSpec::Mocks::ArgumentListMatcher.new(event_name.to_s, *expected_args, **expected_kwargs)
34
+ matcher.args_match?(name, *args, **kwargs)
35
+ end
36
+ end
37
+ end
38
+
39
+ module BroadcastMatcher
40
+ class Matcher
41
+ include ::RSpec::Matchers::Composable
42
+
43
+ def initialize(event, *args, **kwargs)
44
+ @event = event
45
+ @args = args
46
+ @kwargs = kwargs
47
+ end
48
+
49
+ def supports_block_expectations?
50
+ true
51
+ end
52
+
53
+ def matches?(block)
54
+ @event_recorder = EventRecorder.new
55
+
56
+ Wisper.subscribe(@event_recorder, {}) do
57
+ block.call
58
+ end
59
+
60
+ @event_recorder.broadcast?(@event, *@args, **@kwargs)
61
+ end
62
+
63
+ def description
64
+ msg = "broadcast #{@event} event"
65
+ msg += " with args: #{@args.inspect}" if @args.size > 0
66
+ msg += " with kwargs: #{@kwargs.inspect}" unless @kwargs.empty?
67
+ msg
68
+ end
69
+
70
+ def failure_message
71
+ msg = "expected publisher to broadcast #{@event} event"
72
+ msg += " with args: #{@args.inspect}" if @args.size > 0
73
+ msg += " with args: #{@kwargs.inspect}" unless @kwargs.empty?
74
+ msg << broadcast_events_list
75
+ msg
76
+ end
77
+
78
+ def expected
79
+ [] << @args
80
+ [] << @kwargs
81
+ end
82
+
83
+ def actual
84
+ broadcast_events_args
85
+ end
86
+
87
+ def diffable?
88
+ true
89
+ end
90
+
91
+ def failure_message_when_negated
92
+ msg = "expected publisher not to broadcast #{@event} event"
93
+ msg += " with args: #{@args.inspect}" if @args.size > 0
94
+ msg += " with args: #{@kwargs.inspect}" unless @kwargs.empty?
95
+ msg
96
+ end
97
+
98
+ def broadcast_events_args
99
+ @event_recorder.broadcast_events.map {|event| event.size == 1 ? [] : event[1..-1] }
100
+ end
101
+
102
+ def broadcast_events_list
103
+ if @event_recorder.broadcast_events.any?
104
+ " (actual events broadcast: #{event_names.join(', ')})"
105
+ else
106
+ " (no events broadcast)"
107
+ end
108
+ end
109
+ private :broadcast_events_list
110
+
111
+ def event_names
112
+ @event_recorder.broadcast_events.map do |event|
113
+ event.size == 1 ? event[0] : "#{event[0]}(#{event[1..-1].join(", ")})"
114
+ end
115
+ end
116
+ private :event_names
117
+ end
118
+
119
+ def broadcast(event, *args, **kwargs)
120
+ Matcher.new(event, *args, **kwargs)
121
+ end
122
+
123
+ alias_method :publish, :broadcast
124
+ end
125
+ end
126
+
127
+ # Prior to being extracted from Wisper the matcher was namespaced as Rspec,
128
+ # it is now RSpec. This will raise a helpful message for those upgrading to
129
+ # Wisper 2.0
130
+ module Rspec
131
+ module BroadcastMatcher
132
+ def self.included(base)
133
+ raise 'Please include Wisper::RSpec::BroadcastMatcher instead of Wisper::Rspec::BroadcastMatcher (notice the capitalization of RSpec)'
134
+ end
135
+ end
136
+ end
137
+ end
138
+
139
+ ::RSpec::Matchers.define_negated_matcher :not_broadcast, :broadcast
140
+ ::RSpec::Matchers.define_negated_matcher :not_publish, :publish
@@ -0,0 +1,12 @@
1
+ class TestWisperPublisher
2
+ include Wisper::Publisher
3
+ def initialize(*args); end
4
+ end
5
+
6
+ def stub_wisper_publisher(clazz, called_method, event_to_publish, *published_event_args)
7
+ stub_const(clazz, Class.new(TestWisperPublisher) do
8
+ define_method(called_method) do |*args|
9
+ publish(event_to_publish, *published_event_args)
10
+ end
11
+ end)
12
+ end
@@ -0,0 +1,5 @@
1
+ module Wisper
2
+ module Rspec
3
+ VERSION = "2.0.0"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "wisper/rspec/version"
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wisper-rspec-compat
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Kris Leech
8
+ - Jamie Schembri
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2023-02-28 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Rspec matchers and stubbing for Wisper-Compat
15
+ email:
16
+ - kris.leech@gmail.com
17
+ - jamie.schembri@nedap.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - CHANGELOG.md
23
+ - LICENSE.txt
24
+ - README.md
25
+ - bin/console
26
+ - bin/setup
27
+ - lib/wisper/rspec.rb
28
+ - lib/wisper/rspec/matchers.rb
29
+ - lib/wisper/rspec/stub_wisper_publisher.rb
30
+ - lib/wisper/rspec/version.rb
31
+ homepage: https://github.com/nedap/wisper-rspec-compat
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '3'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.4.6
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Rspec matchers and stubbing for Wisper-Compat
54
+ test_files: []