spy 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +5 -0
- data/Gemfile +1 -0
- data/README.md +22 -7
- data/Rakefile +2 -0
- data/lib/spy.rb +39 -6
- data/lib/spy/agency.rb +42 -27
- data/lib/spy/call_log.rb +26 -0
- data/lib/spy/constant.rb +72 -14
- data/lib/spy/core_ext/marshal.rb +1 -0
- data/lib/spy/double.rb +17 -0
- data/lib/spy/nest.rb +27 -0
- data/lib/spy/subroutine.rb +146 -44
- data/lib/spy/version.rb +1 -1
- data/spec/spy/any_instance_spec.rb +518 -0
- data/spec/spy/mock_spec.rb +46 -554
- data/spec/spy/mutate_const_spec.rb +21 -63
- data/spec/spy/null_object_mock_spec.rb +11 -39
- data/spec/spy/partial_mock_spec.rb +3 -62
- data/spec/spy/stash_spec.rb +30 -37
- data/spec/spy/stub_spec.rb +0 -6
- data/spec/spy/to_ary_spec.rb +5 -5
- data/test/integration/test_constant_spying.rb +1 -1
- data/test/integration/test_instance_method.rb +32 -0
- data/test/integration/test_subroutine_spying.rb +7 -4
- data/test/spy/test_double.rb +4 -0
- data/test/spy/test_subroutine.rb +28 -3
- data/test/support/pen.rb +15 -0
- metadata +8 -30
- data/spec/spy/bug_report_10260_spec.rb +0 -8
- data/spec/spy/bug_report_10263_spec.rb +0 -24
- data/spec/spy/bug_report_496_spec.rb +0 -18
- data/spec/spy/bug_report_600_spec.rb +0 -24
- data/spec/spy/bug_report_7611_spec.rb +0 -16
- data/spec/spy/bug_report_8165_spec.rb +0 -31
- data/spec/spy/bug_report_830_spec.rb +0 -21
- data/spec/spy/bug_report_957_spec.rb +0 -22
- data/spec/spy/double_spec.rb +0 -12
- data/spec/spy/failing_argument_matchers_spec.rb +0 -94
- data/spec/spy/options_hash_spec.rb +0 -35
- data/spec/spy/precise_counts_spec.rb +0 -68
- data/spec/spy/stubbed_message_expectations_spec.rb +0 -47
- data/spec/spy/test_double_spec.rb +0 -54
data/spec/spy/double_spec.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
|
3
|
-
describe "double" do
|
4
|
-
it "is an alias for stub and mock" do
|
5
|
-
expect(double()).to be_a(RSpec::Mocks::Mock)
|
6
|
-
end
|
7
|
-
|
8
|
-
it "uses 'Double' in failure messages" do
|
9
|
-
double = double('name')
|
10
|
-
expect {double.foo}.to raise_error(/Double "name" received/)
|
11
|
-
end
|
12
|
-
end
|
@@ -1,94 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "failing MockArgumentMatchers" do
|
6
|
-
before(:each) do
|
7
|
-
@double = double("double")
|
8
|
-
@reporter = double("reporter").as_null_object
|
9
|
-
end
|
10
|
-
|
11
|
-
after(:each) do
|
12
|
-
@double.rspec_reset
|
13
|
-
end
|
14
|
-
|
15
|
-
it "rejects non boolean" do
|
16
|
-
@double.should_receive(:random_call).with(boolean())
|
17
|
-
expect do
|
18
|
-
@double.random_call("false")
|
19
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "rejects non numeric" do
|
23
|
-
@double.should_receive(:random_call).with(an_instance_of(Numeric))
|
24
|
-
expect do
|
25
|
-
@double.random_call("1")
|
26
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "rejects non string" do
|
30
|
-
@double.should_receive(:random_call).with(an_instance_of(String))
|
31
|
-
expect do
|
32
|
-
@double.random_call(123)
|
33
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "rejects goose when expecting a duck" do
|
37
|
-
@double.should_receive(:random_call).with(duck_type(:abs, :div))
|
38
|
-
expect { @double.random_call("I don't respond to :abs or :div") }.to raise_error(RSpec::Mocks::MockExpectationError)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "fails if regexp does not match submitted string" do
|
42
|
-
@double.should_receive(:random_call).with(/bcd/)
|
43
|
-
expect { @double.random_call("abc") }.to raise_error(RSpec::Mocks::MockExpectationError)
|
44
|
-
end
|
45
|
-
|
46
|
-
it "fails if regexp does not match submitted regexp" do
|
47
|
-
@double.should_receive(:random_call).with(/bcd/)
|
48
|
-
expect { @double.random_call(/bcde/) }.to raise_error(RSpec::Mocks::MockExpectationError)
|
49
|
-
end
|
50
|
-
|
51
|
-
it "fails for a hash w/ wrong values" do
|
52
|
-
@double.should_receive(:random_call).with(:a => "b", :c => "d")
|
53
|
-
expect do
|
54
|
-
@double.random_call(:a => "b", :c => "e")
|
55
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, /Double "double" received :random_call with unexpected arguments\n expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)\n got: \(\{(:a=>\"b\", :c=>\"e\"|:c=>\"e\", :a=>\"b\")\}\)/)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "fails for a hash w/ wrong keys" do
|
59
|
-
@double.should_receive(:random_call).with(:a => "b", :c => "d")
|
60
|
-
expect do
|
61
|
-
@double.random_call("a" => "b", "c" => "d")
|
62
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, /Double "double" received :random_call with unexpected arguments\n expected: \(\{(:a=>\"b\", :c=>\"d\"|:c=>\"d\", :a=>\"b\")\}\)\n got: \(\{(\"a\"=>\"b\", \"c\"=>\"d\"|\"c\"=>\"d\", \"a\"=>\"b\")\}\)/)
|
63
|
-
end
|
64
|
-
|
65
|
-
it "matches against a Matcher" do
|
66
|
-
expect do
|
67
|
-
@double.should_receive(:msg).with(equal(3))
|
68
|
-
@double.msg(37)
|
69
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"double\" received :msg with unexpected arguments\n expected: (equal 3)\n got: (37)")
|
70
|
-
end
|
71
|
-
|
72
|
-
it "fails no_args with one arg" do
|
73
|
-
expect do
|
74
|
-
@double.should_receive(:msg).with(no_args)
|
75
|
-
@double.msg(37)
|
76
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"double\" received :msg with unexpected arguments\n expected: (no args)\n got: (37)")
|
77
|
-
end
|
78
|
-
|
79
|
-
it "fails hash_including with missing key" do
|
80
|
-
expect do
|
81
|
-
@double.should_receive(:msg).with(hash_including(:a => 1))
|
82
|
-
@double.msg({})
|
83
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"double\" received :msg with unexpected arguments\n expected: (hash_including(:a=>1))\n got: ({})")
|
84
|
-
end
|
85
|
-
|
86
|
-
it "fails with block matchers" do
|
87
|
-
expect do
|
88
|
-
@double.should_receive(:msg).with {|arg| expect(arg).to eq :received }
|
89
|
-
@double.msg :no_msg_for_you
|
90
|
-
end.to raise_error(RSpec::Expectations::ExpectationNotMetError, /expected: :received.*\s*.*got: :no_msg_for_you/)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "calling :should_receive with an options hash" do
|
6
|
-
it "reports the file and line submitted with :expected_from" do
|
7
|
-
begin
|
8
|
-
mock = RSpec::Mocks::Mock.new("a mock")
|
9
|
-
mock.should_receive(:message, :expected_from => "/path/to/blah.ext:37")
|
10
|
-
mock.rspec_verify
|
11
|
-
rescue Exception => e
|
12
|
-
ensure
|
13
|
-
expect(e.backtrace.to_s).to match /\/path\/to\/blah.ext:37/m
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
it "uses the message supplied with :message" do
|
18
|
-
expect {
|
19
|
-
m = RSpec::Mocks::Mock.new("a mock")
|
20
|
-
m.should_receive(:message, :message => "recebi nada")
|
21
|
-
m.rspec_verify
|
22
|
-
}.to raise_error("recebi nada")
|
23
|
-
end
|
24
|
-
|
25
|
-
it "uses the message supplied with :message after a similar stub" do
|
26
|
-
expect {
|
27
|
-
m = RSpec::Mocks::Mock.new("a mock")
|
28
|
-
Spy.on(m, :message)
|
29
|
-
m.should_receive(:message, :message => "from mock")
|
30
|
-
m.rspec_verify
|
31
|
-
}.to raise_error("from mock")
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "PreciseCounts" do
|
6
|
-
before(:each) do
|
7
|
-
@double = double("test double")
|
8
|
-
end
|
9
|
-
|
10
|
-
it "fails when exactly n times method is called less than n times" do
|
11
|
-
@double.should_receive(:do_something).exactly(3).times
|
12
|
-
@double.do_something
|
13
|
-
@double.do_something
|
14
|
-
expect {
|
15
|
-
@double.rspec_verify
|
16
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "fails fast when exactly n times method is called more than n times" do
|
20
|
-
@double.should_receive(:do_something).exactly(3).times
|
21
|
-
@double.do_something
|
22
|
-
@double.do_something
|
23
|
-
@double.do_something
|
24
|
-
expect {
|
25
|
-
@double.do_something
|
26
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "fails when exactly n times method is never called" do
|
30
|
-
@double.should_receive(:do_something).exactly(3).times
|
31
|
-
expect {
|
32
|
-
@double.rspec_verify
|
33
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "passes if exactly n times method is called exactly n times" do
|
37
|
-
@double.should_receive(:do_something).exactly(3).times
|
38
|
-
@double.do_something
|
39
|
-
@double.do_something
|
40
|
-
@double.do_something
|
41
|
-
@double.rspec_verify
|
42
|
-
end
|
43
|
-
|
44
|
-
it "returns the value given by a block when the exactly once method is called" do
|
45
|
-
@double.should_receive(:to_s).exactly(:once) { "testing" }
|
46
|
-
expect(@double.to_s).to eq "testing"
|
47
|
-
@double.rspec_verify
|
48
|
-
end
|
49
|
-
|
50
|
-
it "passes mutiple calls with different args" do
|
51
|
-
@double.should_receive(:do_something).once.with(1)
|
52
|
-
@double.should_receive(:do_something).once.with(2)
|
53
|
-
@double.do_something(1)
|
54
|
-
@double.do_something(2)
|
55
|
-
@double.rspec_verify
|
56
|
-
end
|
57
|
-
|
58
|
-
it "passes multiple calls with different args and counts" do
|
59
|
-
@double.should_receive(:do_something).twice.with(1)
|
60
|
-
@double.should_receive(:do_something).once.with(2)
|
61
|
-
@double.do_something(1)
|
62
|
-
@double.do_something(2)
|
63
|
-
@double.do_something(1)
|
64
|
-
@double.rspec_verify
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "expection set on previously stubbed method" do
|
4
|
-
it "fails if message is not received after expectation is set" do
|
5
|
-
double = double(:msg => nil)
|
6
|
-
double.msg
|
7
|
-
double.should_receive(:msg)
|
8
|
-
expect { double.rspec_verify }.to raise_error(RSpec::Mocks::MockExpectationError)
|
9
|
-
end
|
10
|
-
|
11
|
-
it "outputs arguments of similar calls" do
|
12
|
-
double = double('double', :foo => true)
|
13
|
-
double.should_receive(:foo).with('first')
|
14
|
-
double.foo('second')
|
15
|
-
double.foo('third')
|
16
|
-
expect {
|
17
|
-
double.rspec_verify
|
18
|
-
}.to raise_error(%Q|Double "double" received :foo with unexpected arguments\n expected: ("first")\n got: ("second"), ("third")|)
|
19
|
-
double.rspec_reset
|
20
|
-
end
|
21
|
-
|
22
|
-
context "with argument constraint on stub" do
|
23
|
-
it "matches any args if no arg constraint set on expectation" do
|
24
|
-
double = double("mock")
|
25
|
-
Spy.on(double, :foo).with(3).and_return("stub")
|
26
|
-
double.should_receive(:foo).at_least(:once).and_return("expectation")
|
27
|
-
double.foo
|
28
|
-
double.rspec_verify
|
29
|
-
end
|
30
|
-
|
31
|
-
it "matches specific args set on expectation" do
|
32
|
-
double = double("mock")
|
33
|
-
Spy.on(double, :foo).with(3).and_return("stub")
|
34
|
-
double.should_receive(:foo).at_least(:once).with(4).and_return("expectation")
|
35
|
-
double.foo(4)
|
36
|
-
double.rspec_verify
|
37
|
-
end
|
38
|
-
|
39
|
-
it "fails if expectation's arg constraint is not met" do
|
40
|
-
double = double("mock")
|
41
|
-
Spy.on(double, :foo).with(3).and_return("stub")
|
42
|
-
double.should_receive(:foo).at_least(:once).with(4).and_return("expectation")
|
43
|
-
double.foo(3)
|
44
|
-
expect { double.rspec_verify }.to raise_error(/expected: \(4\)\s+got: \(3\)/)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Spy
|
4
|
-
describe Double do
|
5
|
-
before(:all) do
|
6
|
-
Module.class_eval do
|
7
|
-
private
|
8
|
-
def use; end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
after(:all) do
|
13
|
-
Module.class_eval do
|
14
|
-
undef use
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
it 'can be extended onto a module to make it a pure test double that can mock private methods' do
|
19
|
-
double = Module.new
|
20
|
-
Spy.on(double, :use)
|
21
|
-
expect { double.use }.to raise_error(/private method `use' called/)
|
22
|
-
|
23
|
-
double = Module.new { TestDouble.extend_onto(self) }
|
24
|
-
double.should_receive(:use).and_return(:ok)
|
25
|
-
expect(double.use).to be(:ok)
|
26
|
-
end
|
27
|
-
|
28
|
-
it 'sets the test double name when a name is passed' do
|
29
|
-
double = Module.new { TestDouble.extend_onto(self, "MyDouble") }
|
30
|
-
expect { double.foo }.to raise_error(/Mock "MyDouble" received/)
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'stubs the methods passed in the stubs hash' do
|
34
|
-
double = Module.new do
|
35
|
-
TestDouble.extend_onto(self, "MyDouble", :a => 5, :b => 10)
|
36
|
-
end
|
37
|
-
|
38
|
-
expect(double.a).to eq(5)
|
39
|
-
expect(double.b).to eq(10)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'indicates what type of test double it is in error messages' do
|
43
|
-
double = Module.new do
|
44
|
-
TestDouble.extend_onto(self, "A", :__declared_as => "ModuleMock")
|
45
|
-
end
|
46
|
-
expect { double.foo }.to raise_error(/ModuleMock "A"/)
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'is declared as a mock by default' do
|
50
|
-
double = Module.new { TestDouble.extend_onto(self) }
|
51
|
-
expect { double.foo }.to raise_error(/Mock received/)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|