rspec-subject_call 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 172687869344e8e7da83eb7ea7af4de7c8606a875cde8d7e329a782d46de45f7
4
+ data.tar.gz: d31a16a91f755940f51516d054cc8b0bcb70e24deea47b6f0faf4df5c9869b36
5
+ SHA512:
6
+ metadata.gz: 034f77cf2098c958e7af7ba4f614cec544d9d2e74078411fefc51d9f608f014cf0eb3d976913dc3a95c85a2759afa1767773b83cfa829ba45b117783d24ddbf3
7
+ data.tar.gz: b4f88c681e6a6fcb3ad81cdff93bc80e713d1db44db6fb77e0cf6f8392c36271e48935dd98c2e9eb69f01a34fa5250496b676d26e94ab942cd74b15786cf00c1
data/Gemfile CHANGED
@@ -3,3 +3,6 @@ source 'https://rubygems.org'
3
3
  #gem 'pry-debugger'
4
4
 
5
5
  gemspec
6
+
7
+ gem 'rspec'
8
+ gem 'simplecov', require: false
data/README.md CHANGED
@@ -19,36 +19,38 @@ make assertions about, such as caching, keeping counts or raising exceptions.
19
19
  To illustrate, here is a class that has all these types of methods.
20
20
 
21
21
  ```ruby
22
- class A
23
- attr :x
22
+ module ReadmeExample
23
+ class A
24
+ attr :x
24
25
 
25
- def initialize(args = {})
26
- @b = args[:b]
27
- @x = 0
28
- end
26
+ def initialize(b: nil)
27
+ @x = 0
28
+ @b = b
29
+ end
29
30
 
30
- def query
31
- 1
32
- end
31
+ def query
32
+ 1
33
+ end
33
34
 
34
- def command
35
- @x = 1
36
- end
35
+ def command
36
+ @x = 1
37
+ end
37
38
 
38
- def query_command
39
- @x = 1
40
- 1
41
- end
39
+ def query_command
40
+ @x = 1
41
+ 1
42
+ end
42
43
 
43
- def query_command_side_effect
44
- @b.command
45
- @x = 1
46
- 1
44
+ def query_command_side_effect
45
+ @b.command
46
+ @x = 1
47
+ 1
48
+ end
47
49
  end
48
- end
49
50
 
50
- class B
51
- def command
51
+ class B
52
+ def command
53
+ end
52
54
  end
53
55
  end
54
56
  ```
@@ -60,21 +62,21 @@ anything elegantly. Right?
60
62
  With vanilla RSpec, I might test this method like this:
61
63
 
62
64
  ```ruby
63
- describe A do
64
- let(:a) { A.new(b: b) }
65
+ describe "ReadmeExample::A, vanilla" do
66
+ let(:a) { ReadmeExample::A.new(b: b) }
65
67
  let(:b) { double('b').as_null_object }
66
68
 
67
69
  describe '#query_command_side_effect' do
68
- it 'should return 1' do
69
- a.query_command_side_effect.should == 1
70
+ it 'is expected to return 1' do
71
+ expect(a.query_command_side_effect).to eq(1)
70
72
  end
71
73
 
72
- it 'should update x' do
74
+ it 'is expected to update x' do
73
75
  expect { a.query_command_side_effect }.to change(a, :x)
74
76
  end
75
77
 
76
- it 'should call command on b' do
77
- b.should_receive(:command).once
78
+ it 'is expected to call command on b' do
79
+ expect(b).to receive(:command).once
78
80
  a.query_command_side_effect
79
81
  end
80
82
  end
@@ -91,15 +93,15 @@ This is how I would like to test this tricky method:
91
93
  ```ruby
92
94
  require 'rspec/subject_call'
93
95
 
94
- describe A do
95
- let(:a) { A.new(b: b) }
96
+ describe "ReadmeExample::A, subject_call style" do
97
+ let(:a) { ReadmeExample::A.new(b: b) }
96
98
  let(:b) { double('b').as_null_object }
97
99
 
98
100
  describe '#query_command_side_effect' do
99
101
  subject { a.query_command_side_effect }
100
- it { should == 1 }
101
- call { should change(a, :x) }
102
- call { should meet_expectations { b.should_receive(:command) } }
102
+ it { is_expected.to eq(1) }
103
+ call { is_expected.to change(a, :x) }
104
+ call { is_expected.to meet_expectations { expect(b).to receive(:command) } }
103
105
  end
104
106
  end
105
107
  ```
@@ -111,5 +113,25 @@ already packaged up for you and ready to use.
111
113
 
112
114
  With this gem, the above example passes.
113
115
 
116
+ ## Installation
117
+
118
+ ```
119
+ gem install rspec-subject_call
120
+ ```
121
+
122
+ If you prefer, copy and paste this into your `Gemfile` and run `bundle`:
123
+
124
+ ```
125
+ gem 'rspec-subject_call'
126
+ ```
127
+
128
+ Later that day, ensure this line is included somehow before your spec:
129
+
130
+ ```
131
+ require 'rspec/subject_call'
132
+ ```
133
+
134
+ Now you can use your new powers for good or for awesome.
135
+
114
136
 
115
137
  [1]: http://en.wikipedia.org/wiki/Command%E2%80%93query_separation "Command-Query Separation"
@@ -1,25 +1,21 @@
1
1
  require 'rspec/subject_call/matchers/meet_expectations_matcher'
2
- require 'rspec/subject_call/matchers/return_value_matcher'
3
2
 
4
3
  module RSpec
5
4
  module SubjectCall
6
5
  module ExampleGroupClassMethods
7
- # Define a method +subject+ for use inside examples, and also a method
8
- # +call+ which returns a lambda containing the subject, suitable for use
9
- # with matchers that take lambdas.
6
+ # Define a method +subject+ for use inside example groups which provides
7
+ # a +call+ method for examples in this group which returns a lambda
8
+ # containing the subject, suitable for use with matchers that take
9
+ # lambdas, such as +change+.
10
10
  #
11
11
  # e.g.
12
12
  #
13
13
  # subject { obj.my_method }
14
14
  #
15
- # it { should == some_result }
15
+ # it { is_expected.to eq(some_result) }
16
16
  #
17
- # it 'should change something, should syntax' do
18
- # call.should change{something}
19
- # end
20
- #
21
- # it 'should change something, expect syntax' do
22
- # expect(call).to change{something}
17
+ # it 'is expected to change something' do
18
+ # expect(call).to change { something }
23
19
  # end
24
20
  #
25
21
  def subject(name=nil, &block)
@@ -36,39 +32,11 @@ module RSpec
36
32
  super(name, &block)
37
33
  end
38
34
 
39
- # Allow for syntax similar to +its+:
40
- #
41
- # its(:my_method) { should == 1 }
42
- # calling(:my_method) { should change{something} }
43
- #
44
- def calling(method_name, &block)
45
- describe(method_name) do
46
- let(:__its_subject) do
47
- method_chain = method_name.to_s.split('.')
48
- lambda do
49
- method_chain.inject(subject) do |inner_subject, attr|
50
- inner_subject.send(attr)
51
- end
52
- end
53
- end
54
-
55
- def should(matcher=nil, message=nil)
56
- RSpec::Expectations::PositiveExpectationHandler.handle_matcher(__its_subject, matcher, message)
57
- end
58
-
59
- def should_not(matcher=nil, message=nil)
60
- RSpec::Expectations::NegativeExpectationHandler.handle_matcher(__its_subject, matcher, message)
61
- end
62
-
63
- example(&block)
64
- end
65
- end
66
-
67
35
  # Like +it+ but sets the implicit subject to the lambda you supplied when
68
36
  # defining the subject, so that you can use it with matchers that take
69
37
  # blocks like +change+:
70
38
  #
71
- # call { should change{something} }
39
+ # call { is_expected.to change { something } }
72
40
  #
73
41
  def call(desc=nil, *args, &block)
74
42
  # Create a new example, where the subject is set to the subject block,
@@ -76,11 +44,7 @@ module RSpec
76
44
  example do
77
45
  self.class.class_eval do
78
46
  define_method(:subject) do
79
- if defined?(@_subject_call)
80
- @_subject_call
81
- else
82
- @_subject_call = call
83
- end
47
+ call # calls define_method(:call) inside def subject above
84
48
  end
85
49
  end
86
50
  instance_eval(&block)
@@ -1,15 +1,19 @@
1
1
  module RSpec
2
2
  module SubjectCall
3
3
  module Matchers
4
- # A general purpose matcher that inverts order of operations
5
- # allowing for one liners involving mock expectations.
4
+ # A general purpose matcher that inverts order of operations.
5
+ #
6
+ # e.g.
7
+ #
8
+ # expect { A.new(b).method_call_with_side_effect }.to meet_expectations { expect(b).to receive(:command) } }
9
+ #
6
10
  class MeetExpectationsMatcher
7
11
  def initialize(&block)
8
- @should_receives = block
12
+ @expected_receives = block
9
13
  end
10
14
 
11
15
  def matches?(subject)
12
- @should_receives.call # e.g. x.should_receive(:y)
16
+ @expected_receives.call # e.g. expect(x).to receive(:y)
13
17
  subject.call # execute the subject (assumed to be a Proc)
14
18
  end
15
19
 
@@ -1,5 +1,5 @@
1
1
  module RSpec
2
2
  module SubjectCall
3
- VERSION = '1.0.1'
3
+ VERSION = '1.0.2'
4
4
  end
5
5
  end
@@ -4,14 +4,13 @@ Gem::Specification.new do |s|
4
4
  s.name = 'rspec-subject_call'
5
5
  s.version = RSpec::SubjectCall::VERSION
6
6
  s.platform = Gem::Platform::RUBY
7
+ s.license = 'MIT'
7
8
  s.authors = [ 'Gregory McIntyre' ]
8
9
  s.email = [ 'greg@gregorymcintyre.com' ]
9
- s.homepage = 'http://github.com/rails-oceania/rspec-subject_call'
10
- s.description = 'Lets you use the subject in convenient ways with block matchers like "change" and "receive_error"'
10
+ s.homepage = 'http://github.com/puyo/rspec-subject_call'
11
+ s.description = 'Use the rspec subject more conveniently'
11
12
  s.summary = "rspec-subject_call-#{s.version}"
12
- s.required_rubygems_version = '> 1.3.6'
13
13
 
14
- s.files = `git ls-files`.split($\)
15
- s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
14
+ s.files = `git ls-files`.split($\) - ['.gitignore']
16
15
  s.require_path = 'lib'
17
16
  end
@@ -1,51 +1,55 @@
1
- class A
2
- attr :x
1
+ require 'spec_helper'
3
2
 
4
- def initialize(args = {})
5
- @b = args[:b]
6
- @x = 0
7
- end
3
+ module ReadmeExample
4
+ class A
5
+ attr :x
8
6
 
9
- def query
10
- 1
11
- end
7
+ def initialize(b: nil)
8
+ @x = 0
9
+ @b = b
10
+ end
12
11
 
13
- def command
14
- @x = 1
15
- end
12
+ def query
13
+ 1
14
+ end
16
15
 
17
- def query_command
18
- @x = 1
19
- 1
20
- end
16
+ def command
17
+ @x = 1
18
+ end
19
+
20
+ def query_command
21
+ @x = 1
22
+ 1
23
+ end
21
24
 
22
- def query_command_side_effect
23
- @b.command
24
- @x = 1
25
- 1
25
+ def query_command_side_effect
26
+ @b.command
27
+ @x = 1
28
+ 1
29
+ end
26
30
  end
27
- end
28
31
 
29
- class B
30
- def command
32
+ class B
33
+ def command
34
+ end
31
35
  end
32
36
  end
33
37
 
34
- describe A do
35
- let(:a) { A.new(b: b) }
38
+ describe "ReadmeExample::A, vanilla" do
39
+ let(:a) { ReadmeExample::A.new(b: b) }
36
40
  let(:b) { double('b').as_null_object }
37
41
 
38
42
  describe '#query_command_side_effect' do
39
- it 'should return 1' do
40
- a.query_command_side_effect.should == 1
43
+ it 'is expected to return 1' do
44
+ expect(a.query_command_side_effect).to eq(1)
41
45
  end
42
46
 
43
- it 'should update x' do
47
+ it 'is expected to update x' do
44
48
  expect { a.query_command_side_effect }.to change(a, :x)
45
49
  end
46
50
 
47
- it 'should call command on b' do
48
- b.should_receive(:command).once
51
+ it 'is expected to call command on b' do
52
+ expect(b).to receive(:command).once
49
53
  a.query_command_side_effect
50
54
  end
51
55
  end
@@ -53,14 +57,14 @@ end
53
57
 
54
58
  require 'rspec/subject_call'
55
59
 
56
- describe A do
57
- let(:a) { A.new(b: b) }
60
+ describe "ReadmeExample::A, subject_call style" do
61
+ let(:a) { ReadmeExample::A.new(b: b) }
58
62
  let(:b) { double('b').as_null_object }
59
63
 
60
64
  describe '#query_command_side_effect' do
61
65
  subject { a.query_command_side_effect }
62
- it { should == 1 }
63
- call { should change(a, :x) }
64
- call { should meet_expectations { b.should_receive(:command) } }
66
+ it { is_expected.to eq(1) }
67
+ call { is_expected.to change(a, :x) }
68
+ call { is_expected.to meet_expectations { expect(b).to receive(:command) } }
65
69
  end
66
70
  end
@@ -1,33 +1,37 @@
1
- class A
2
- attr_reader :counter
1
+ require 'spec_helper'
3
2
 
4
- def initialize(args = {})
5
- @b = args[:b]
6
- @counter = 0
7
- end
3
+ module RSpecSubjectCallSpec
4
+ class A
5
+ attr_reader :counter
8
6
 
9
- def query
10
- 1
11
- end
7
+ def initialize(args = {})
8
+ @b = args[:b]
9
+ @counter = 0
10
+ end
12
11
 
13
- def command
14
- @b.command
15
- end
12
+ def query
13
+ 1
14
+ end
16
15
 
17
- def query_command
18
- @b.command
19
- 1
20
- end
16
+ def command
17
+ @b.command
18
+ end
21
19
 
22
- def query_command_side_effect
23
- @b.command
24
- @counter += 1
25
- 1
20
+ def query_command
21
+ @b.command
22
+ 1
23
+ end
24
+
25
+ def query_command_side_effect
26
+ @b.command
27
+ @counter += 1
28
+ 1
29
+ end
26
30
  end
27
- end
28
31
 
29
- class B
30
- def command
32
+ class B
33
+ def command
34
+ end
31
35
  end
32
36
  end
33
37
 
@@ -35,45 +39,45 @@ end
35
39
 
36
40
  require 'rspec/subject_call'
37
41
 
38
- describe A do
39
- subject(:a) { A.new(b: b) }
42
+ describe RSpecSubjectCallSpec::A do
43
+ subject(:a) { RSpecSubjectCallSpec::A.new(b: b) }
40
44
  let(:b) { double('b').as_null_object }
41
45
 
42
46
  describe '#query' do
43
- it 'should return 1' do
44
- a.query.should == 1
47
+ it 'is expected to return 1' do
48
+ expect(a.query).to eq(1)
45
49
  end
46
50
  end
47
51
 
48
52
  describe '#command' do
49
- it 'should call command on b' do
50
- b.should_receive(:command).once
53
+ it 'is expected to call command on b' do
54
+ expect(b).to receive(:command).once
51
55
  a.command
52
56
  end
53
57
  end
54
58
 
55
59
  describe '#query_command' do
56
- it 'should return 1' do
57
- a.query_command.should == 1
60
+ it 'is expected to return 1' do
61
+ expect(a.query_command).to eq(1)
58
62
  end
59
63
 
60
- it 'should call command on b' do
61
- b.should_receive(:command).once
64
+ it 'is expected to call command on b' do
65
+ expect(b).to receive(:command).once
62
66
  a.query_command
63
67
  end
64
68
  end
65
69
 
66
70
  describe '#query_command_side_effect - vanilla' do
67
- it 'should return 1' do
68
- a.query_command_side_effect.should == 1
71
+ it 'is expected to return 1' do
72
+ expect(a.query_command_side_effect).to eq(1)
69
73
  end
70
74
 
71
- it 'should call command on b' do
72
- b.should_receive(:command).once
75
+ it 'is expected to call command on b' do
76
+ expect(b).to receive(:command).once
73
77
  a.query_command_side_effect
74
78
  end
75
79
 
76
- it 'should increment counter' do
80
+ it 'is expected to increment counter' do
77
81
  expect { a.query_command_side_effect }.to change(a, :counter).by(1)
78
82
  end
79
83
  end
@@ -81,46 +85,39 @@ describe A do
81
85
  describe '#query_command_side_effect - subject_call with custom doc strings' do
82
86
  subject { a.query_command_side_effect }
83
87
 
84
- it 'should return 1' do
85
- subject.should == 1
88
+ it 'is expected to return 1' do
89
+ expect(subject).to eq(1)
86
90
  end
87
91
 
88
- it 'should call b.command' do
89
- b.should_receive(:command)
92
+ it 'is expected to call b.command' do
93
+ expect(b).to receive(:command).once
90
94
  call_subject
91
95
  end
92
96
 
93
- it 'should increment counter by 1' do
94
- call { should change(a, :counter).by(1) }
97
+ it 'is expected to increment counter by 1' do
98
+ call { is_expected.to change(a, :counter).by(1) }
95
99
  end
96
100
  end
97
101
 
98
102
  describe '#query_command_side_effect - subject_call with one liners' do
99
103
  subject { a.query_command_side_effect }
100
- it { should == 1 }
101
- call { should change(a, :counter).by(1) }
102
- call { should meet_expectations { b.should_receive(:command) } }
103
- end
104
-
105
- describe '#query_command_side_effect - subject_call, its style' do
106
- subject { a }
107
- its(:query_command_side_effect) { should == 1 }
108
- calling(:query_command_side_effect) { should change(a, :counter).by(1) }
109
- calling(:query_command_side_effect) { should meet_expectations { b.should_receive(:command) } }
104
+ it { is_expected.to eq(1) }
105
+ call { is_expected.to change(a, :counter).by(1) }
106
+ call { is_expected.to meet_expectations { expect(b).to receive(:command).once } }
110
107
  end
111
108
 
112
109
  describe '#query_command_side_effect - expect syntax' do
113
110
  subject { a.query_command_side_effect }
114
111
 
115
- it 'should return 1' do
112
+ it 'is expected to return 1' do
116
113
  expect(subject).to eq(1)
117
114
  end
118
115
 
119
- it 'should call b.command' do
120
- expect(call).to meet_expectations { b.should_receive(:command) }
116
+ it 'is expected to call b.command' do
117
+ expect(call).to meet_expectations { expect(b).to receive(:command).once }
121
118
  end
122
119
 
123
- it 'should increment counter by 1' do
120
+ it 'is expected to increment counter by 1' do
124
121
  expect(call).to change(a, :counter).by(1)
125
122
  end
126
123
  end
@@ -0,0 +1,3 @@
1
+ require 'simplecov'
2
+
3
+ SimpleCov.start
metadata CHANGED
@@ -1,58 +1,53 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-subject_call
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
5
- prerelease:
4
+ version: 1.0.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Gregory McIntyre
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-08-04 00:00:00.000000000 Z
11
+ date: 2020-04-27 00:00:00.000000000 Z
13
12
  dependencies: []
14
- description: Lets you use the subject in convenient ways with block matchers like
15
- "change" and "receive_error"
13
+ description: Use the rspec subject more conveniently
16
14
  email:
17
15
  - greg@gregorymcintyre.com
18
16
  executables: []
19
17
  extensions: []
20
18
  extra_rdoc_files: []
21
19
  files:
22
- - .rspec
20
+ - ".rspec"
23
21
  - Gemfile
24
- - Gemfile.lock
25
22
  - README.md
26
23
  - lib/rspec/subject_call.rb
27
24
  - lib/rspec/subject_call/matchers/meet_expectations_matcher.rb
28
- - lib/rspec/subject_call/matchers/return_value_matcher.rb
29
25
  - lib/rspec/subject_call/version.rb
30
26
  - rspec-subject_call.gemspec
31
27
  - spec/readme_example_spec.rb
32
28
  - spec/rspec_subject_call_spec.rb
33
- homepage: http://github.com/rails-oceania/rspec-subject_call
34
- licenses: []
29
+ - spec/spec_helper.rb
30
+ homepage: http://github.com/puyo/rspec-subject_call
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
35
34
  post_install_message:
36
35
  rdoc_options: []
37
36
  require_paths:
38
37
  - lib
39
38
  required_ruby_version: !ruby/object:Gem::Requirement
40
- none: false
41
39
  requirements:
42
- - - ! '>='
40
+ - - ">="
43
41
  - !ruby/object:Gem::Version
44
42
  version: '0'
45
43
  required_rubygems_version: !ruby/object:Gem::Requirement
46
- none: false
47
44
  requirements:
48
- - - ! '>'
45
+ - - ">="
49
46
  - !ruby/object:Gem::Version
50
- version: 1.3.6
47
+ version: '0'
51
48
  requirements: []
52
- rubyforge_project:
53
- rubygems_version: 1.8.24
49
+ rubygems_version: 3.1.2
54
50
  signing_key:
55
- specification_version: 3
56
- summary: rspec-subject_call-1.0.1
51
+ specification_version: 4
52
+ summary: rspec-subject_call-1.0.2
57
53
  test_files: []
58
- has_rdoc:
@@ -1,14 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- rspec-subject_call (1.0.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
-
10
- PLATFORMS
11
- ruby
12
-
13
- DEPENDENCIES
14
- rspec-subject_call!
@@ -1,24 +0,0 @@
1
- require 'rspec/matchers/pretty'
2
- require 'rspec/matchers/built_in'
3
-
4
- module RSpec
5
- module SubjectCall
6
- module Matchers
7
- class ReturnValueMatcher < RSpec::Matchers::BuiltIn::Eq
8
- def matches?(subject)
9
- @actual = subject.call
10
- end
11
-
12
- def description
13
- "return #{expected.inspect}"
14
- end
15
- end
16
- end
17
- end
18
-
19
- module Matchers
20
- def return_value(expected)
21
- ::RSpec::SubjectCall::Matchers::ReturnValueMatcher.new(expected)
22
- end
23
- end
24
- end