wisper-rspec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8e6d23dd1020653426db699b54fc2a1b42303bc8
4
+ data.tar.gz: 456ec8f7f24ec51cb10848addfbb11ebf1ee1737
5
+ SHA512:
6
+ metadata.gz: 40882bd92bd01b9c8be1efd98ba72409c5dab9d0f7b4d1bac38d095db7bb659986c63909b697a8954412d33242a2cb90f48f8eb93b8c522034529a1e17ebbc45
7
+ data.tar.gz: 9ed7cd318f57629d129e8175008dd248478033547695cfb7405c60dcb188a38592c77382708bafc58ea560ba3cd59a2752aeff0eb2964bbb32097f65dee60a51
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ script: rspec spec
3
+ rvm:
4
+ - '1.9.3'
5
+ - '2.1.2'
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -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.
@@ -0,0 +1,104 @@
1
+ # Wisper::Rspec
2
+
3
+ WIP - This is being extracted from Wisper.
4
+
5
+ Rspec matcher and stubbing for Wisper
6
+
7
+ [![Gem Version](https://badge.fury.io/rb/wisper-rspec.png)](http://badge.fury.io/rb/wisper-rspec)
8
+ [![Build Status](https://travis-ci.org/krisleech/wisper-rspec.png?branch=master)](https://travis-ci.org/krisleech/wisper-rspec)
9
+
10
+ ## Installation
11
+
12
+ ```ruby
13
+ gem 'wisper-rspec', require: false
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ### Broadcast Matcher
19
+
20
+ In `spec_helper`
21
+
22
+ ```ruby
23
+ require 'wisper/rspec/matchers'
24
+
25
+ RSpec::configure do |config|
26
+ config.include(Wisper::RSpec::BroadcastMatcher)
27
+ end
28
+ ```
29
+
30
+ In your specs:
31
+
32
+ ```ruby
33
+ expect { publisher.execute }.to broadcast(:an_event)
34
+ ```
35
+
36
+ ### Using message expections
37
+
38
+ If you need to assert on the arguments broadcast you can subscribe a double
39
+ with a [message expection](https://github.com/rspec/rspec-mocks#message-expectations)
40
+ and then use any of the [argument matchers](https://github.com/rspec/rspec-mocks#argument-matchers).
41
+
42
+ ```ruby
43
+ listener = double('Listener')
44
+
45
+ expect(listener).to receive(:an_event).with(some_args)
46
+
47
+ publisher.subscribe(listener)
48
+
49
+ publisher.execute
50
+ ```
51
+
52
+ ### Stubbing publishers
53
+
54
+ You can stub publishers and their events in unit (isolated) tests that only care about reacting to events.
55
+
56
+ Given this piece of code:
57
+
58
+ ```ruby
59
+ class MyController
60
+ def create
61
+ publisher = MyPublisher.new
62
+
63
+ publisher.on(:some_event) do |variable|
64
+ return "Hello with #{variable}!"
65
+ end
66
+
67
+ publisher.execute
68
+ end
69
+ end
70
+ ```
71
+
72
+ You can test it like this:
73
+
74
+ ```ruby
75
+ require 'wisper/rspec/stub_wisper_publisher'
76
+
77
+ describe MyController do
78
+ context "on some_event" do
79
+ before do
80
+ stub_wisper_publisher("MyPublisher", :execute, :some_event, "foo")
81
+ end
82
+
83
+ it "renders" do
84
+ response = MyController.new.create
85
+ expect(response).to eq "Hello with foo!"
86
+ end
87
+ end
88
+ end
89
+ ```
90
+
91
+ This is useful when testing Rails controllers in isolation from the business logic.
92
+
93
+ You can use any number of args to pass to the event:
94
+
95
+ ```ruby
96
+ stub_wisper_publisher("MyPublisher", :execute, :some_event, "foo1", "foo2", ...)
97
+ ```
98
+
99
+ See `spec/lib/rspec_extensions_spec.rb` for a runnable example.
100
+
101
+
102
+ ## Contributing
103
+
104
+ Yes, please.
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1 @@
1
+ require "wisper/rspec/version"
@@ -0,0 +1,57 @@
1
+ require 'rspec/expectations'
2
+
3
+ module Wisper
4
+ module RSpec
5
+ class EventRecorder
6
+ def initialize
7
+ @broadcast_events = []
8
+ end
9
+
10
+ def respond_to?(method_name)
11
+ true
12
+ end
13
+
14
+ def method_missing(method_name, *args, &block)
15
+ @broadcast_events << method_name.to_s
16
+ end
17
+
18
+ def broadcast?(event_name)
19
+ @broadcast_events.include?(event_name.to_s)
20
+ end
21
+ end
22
+
23
+ module BroadcastMatcher
24
+ class Matcher
25
+ def initialize(event)
26
+ @event = event
27
+ end
28
+
29
+ def supports_block_expectations?
30
+ true
31
+ end
32
+
33
+ def matches?(block)
34
+ event_recorder = EventRecorder.new
35
+
36
+ Wisper.subscribe(event_recorder) do
37
+ block.call
38
+ end
39
+
40
+ event_recorder.broadcast?(@event)
41
+ end
42
+
43
+ def failure_message
44
+ "expected publisher to broadcast #{@event} event"
45
+ end
46
+
47
+ def failure_message_when_negated
48
+ "expected publisher not to broadcast #{@event} event"
49
+ end
50
+ end
51
+
52
+ def broadcast(event)
53
+ Matcher.new(event)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -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 = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,26 @@
1
+ RSpec.configure do |config|
2
+ config.expect_with :rspec do |expectations|
3
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
4
+ end
5
+
6
+ config.mock_with :rspec do |mocks|
7
+ mocks.verify_partial_doubles = true
8
+ end
9
+
10
+ config.filter_run :focus
11
+ config.run_all_when_everything_filtered = true
12
+
13
+ # config.disable_monkey_patching!
14
+
15
+ config.warnings = true
16
+
17
+ if config.files_to_run.one?
18
+ config.default_formatter = 'doc'
19
+ end
20
+
21
+ config.profile_examples = 10
22
+
23
+ config.order = :random
24
+
25
+ Kernel.srand config.seed
26
+ end
@@ -0,0 +1,18 @@
1
+ require 'wisper/rspec/matchers'
2
+
3
+ RSpec::configure do |config|
4
+ config.include(Wisper::RSpec::BroadcastMatcher)
5
+ end
6
+
7
+ describe 'broadcast matcher' do
8
+ let(:publisher_class) { Class.new { include Wisper::Publisher } }
9
+ let(:publisher) { publisher_class.new }
10
+
11
+ it 'passes when publisher broadcasts inside block' do
12
+ expect { publisher.send(:broadcast, :foobar) }.to broadcast(:foobar)
13
+ end
14
+
15
+ it 'passes with not_to when publisher does not broadcast inside block' do
16
+ expect { publisher }.not_to broadcast(:foobar)
17
+ end
18
+ end
@@ -0,0 +1,27 @@
1
+ require 'wisper'
2
+ require 'wisper/rspec/stub_wisper_publisher'
3
+
4
+ describe '#stub_wisper_publisher' do
5
+ describe "given a piece of code invoking a publisher" do
6
+ class CodeThatReactsToEvents
7
+ def do_something
8
+ publisher = MyPublisher.new
9
+ publisher.on(:some_event) do |variable1, variable2|
10
+ return "Hello with #{variable1} #{variable2}!"
11
+ end
12
+ publisher.execute
13
+ end
14
+ end
15
+
16
+ context "when stubbing the publisher to emit an event" do
17
+ before do
18
+ stub_wisper_publisher("MyPublisher", :execute, :some_event, "foo1", "foo2")
19
+ end
20
+
21
+ it "emits the event" do
22
+ response = CodeThatReactsToEvents.new.do_something
23
+ expect(response).to eq "Hello with foo1 foo2!"
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wisper/rspec/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "wisper-rspec"
8
+ spec.version = Wisper::Rspec::VERSION
9
+ spec.authors = ["Kris Leech"]
10
+ spec.email = ["kris.leech@gmail.com"]
11
+ spec.summary = "Rspec matchers and stubbing for Wisper"
12
+ spec.description = "Rspec matchers and stubbing for Wisper"
13
+ spec.homepage = "https://github.com/krisleech/wisper-rspec"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency 'wisper'
22
+ spec.add_development_dependency 'rspec'
23
+ spec.add_development_dependency 'bundler'
24
+ spec.add_development_dependency 'rake'
25
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wisper-rspec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kris Leech
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: wisper
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Rspec matchers and stubbing for Wisper
70
+ email:
71
+ - kris.leech@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/wisper/rspec.rb
84
+ - lib/wisper/rspec/matchers.rb
85
+ - lib/wisper/rspec/stub_wisper_publisher.rb
86
+ - lib/wisper/rspec/version.rb
87
+ - spec/spec_helper.rb
88
+ - spec/wisper/rspec/matchers_spec.rb
89
+ - spec/wisper/rspec/stub_wisper_publisher_spec.rb
90
+ - wisper-rspec.gemspec
91
+ homepage: https://github.com/krisleech/wisper-rspec
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.2.2
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Rspec matchers and stubbing for Wisper
115
+ test_files:
116
+ - spec/spec_helper.rb
117
+ - spec/wisper/rspec/matchers_spec.rb
118
+ - spec/wisper/rspec/stub_wisper_publisher_spec.rb