rspec-mocks 3.0.0.beta2 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Changelog.md +104 -36
- data/README.md +35 -4
- data/lib/rspec/mocks.rb +52 -13
- data/lib/rspec/mocks/any_instance.rb +10 -0
- data/lib/rspec/mocks/any_instance/chain.rb +17 -0
- data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +1 -1
- data/lib/rspec/mocks/any_instance/expectation_chain.rb +9 -6
- data/lib/rspec/mocks/any_instance/message_chains.rb +10 -1
- data/lib/rspec/mocks/any_instance/proxy.rb +112 -0
- data/lib/rspec/mocks/any_instance/recorder.rb +49 -18
- data/lib/rspec/mocks/any_instance/stub_chain.rb +5 -3
- data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +1 -1
- data/lib/rspec/mocks/argument_list_matcher.rb +5 -3
- data/lib/rspec/mocks/argument_matchers.rb +28 -12
- data/lib/rspec/mocks/configuration.rb +29 -0
- data/lib/rspec/mocks/error_generator.rb +2 -5
- data/lib/rspec/mocks/example_methods.rb +122 -13
- data/lib/rspec/mocks/instance_method_stasher.rb +26 -2
- data/lib/rspec/mocks/marshal_extension.rb +41 -0
- data/lib/rspec/mocks/matchers/expectation_customization.rb +20 -0
- data/lib/rspec/mocks/matchers/receive.rb +6 -20
- data/lib/rspec/mocks/matchers/receive_message_chain.rb +7 -5
- data/lib/rspec/mocks/matchers/receive_messages.rb +1 -2
- data/lib/rspec/mocks/message_chain.rb +9 -9
- data/lib/rspec/mocks/message_expectation.rb +6 -5
- data/lib/rspec/mocks/method_double.rb +67 -10
- data/lib/rspec/mocks/method_reference.rb +1 -1
- data/lib/rspec/mocks/mutate_const.rb +10 -14
- data/lib/rspec/mocks/object_reference.rb +7 -4
- data/lib/rspec/mocks/proxy.rb +95 -8
- data/lib/rspec/mocks/space.rb +58 -9
- data/lib/rspec/mocks/syntax.rb +128 -210
- data/lib/rspec/mocks/targets.rb +6 -8
- data/lib/rspec/mocks/test_double.rb +16 -25
- data/lib/rspec/mocks/verifying_double.rb +15 -12
- data/lib/rspec/mocks/verifying_message_expecation.rb +3 -3
- data/lib/rspec/mocks/verifying_proxy.rb +23 -17
- data/lib/rspec/mocks/version.rb +3 -1
- metadata +29 -257
- metadata.gz.sig +0 -0
- data/features/README.md +0 -75
- data/features/Scope.md +0 -17
- data/features/Upgrade.md +0 -22
- data/features/argument_matchers/README.md +0 -27
- data/features/argument_matchers/explicit.feature +0 -59
- data/features/argument_matchers/general_matchers.feature +0 -85
- data/features/argument_matchers/type_matchers.feature +0 -26
- data/features/message_expectations/README.md +0 -75
- data/features/message_expectations/allow_any_instance_of.feature +0 -26
- data/features/message_expectations/any_instance.feature +0 -43
- data/features/message_expectations/block_local_expectations.feature.pending +0 -55
- data/features/message_expectations/call_original.feature +0 -23
- data/features/message_expectations/expect_message_using_expect.feature +0 -107
- data/features/message_expectations/expect_message_using_should_receive.feature +0 -118
- data/features/message_expectations/message_chains_using_expect.feature +0 -49
- data/features/message_expectations/receive_counts.feature +0 -209
- data/features/message_expectations/warn_when_expectation_is_set_on_nil.feature +0 -50
- data/features/method_stubs/README.md +0 -77
- data/features/method_stubs/allow_any_instance_of.feature +0 -136
- data/features/method_stubs/as_null_object.feature +0 -40
- data/features/method_stubs/receive_message_chain.feature +0 -51
- data/features/method_stubs/simple_return_value_with_allow.feature +0 -44
- data/features/method_stubs/simple_return_value_with_stub.feature +0 -64
- data/features/method_stubs/stub_implementation.feature +0 -48
- data/features/method_stubs/to_ary.feature +0 -51
- data/features/mutating_constants/README.md +0 -82
- data/features/mutating_constants/hiding_defined_constant.feature +0 -64
- data/features/mutating_constants/stub_defined_constant.feature +0 -78
- data/features/mutating_constants/stub_undefined_constant.feature +0 -50
- data/features/outside_rspec/standalone.feature +0 -33
- data/features/spies/spy_partial_mock_method.feature +0 -34
- data/features/spies/spy_pure_mock_method.feature +0 -76
- data/features/spies/spy_unstubbed_method.feature +0 -18
- data/features/step_definitions/additional_cli_steps.rb +0 -11
- data/features/support/env.rb +0 -22
- data/features/support/rubinius.rb +0 -6
- data/features/test_frameworks/test_unit.feature +0 -54
- data/features/verifying_doubles/README.md +0 -17
- data/features/verifying_doubles/class_doubles.feature +0 -73
- data/features/verifying_doubles/dynamic_classes.feature +0 -71
- data/features/verifying_doubles/instance_doubles.feature +0 -103
- data/features/verifying_doubles/object_doubles.feature +0 -65
- data/features/verifying_doubles/partial_doubles.feature +0 -34
- data/lib/rspec/mocks/extensions/marshal.rb +0 -17
- data/lib/rspec/mocks/framework.rb +0 -35
- data/lib/rspec/mocks/method_signature_verifier.rb +0 -207
- data/lib/rspec/mocks/ruby_features.rb +0 -24
- data/spec/rspec/mocks/and_call_original_spec.rb +0 -268
- data/spec/rspec/mocks/and_return_spec.rb +0 -23
- data/spec/rspec/mocks/and_yield_spec.rb +0 -126
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +0 -41
- data/spec/rspec/mocks/any_instance_spec.rb +0 -1045
- data/spec/rspec/mocks/array_including_matcher_spec.rb +0 -41
- data/spec/rspec/mocks/at_least_spec.rb +0 -123
- data/spec/rspec/mocks/at_most_spec.rb +0 -90
- data/spec/rspec/mocks/before_all_spec.rb +0 -132
- data/spec/rspec/mocks/block_return_value_spec.rb +0 -70
- data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +0 -204
- data/spec/rspec/mocks/configuration_spec.rb +0 -242
- data/spec/rspec/mocks/double_spec.rb +0 -874
- data/spec/rspec/mocks/example_methods_spec.rb +0 -13
- data/spec/rspec/mocks/extensions/marshal_spec.rb +0 -54
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +0 -184
- data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +0 -67
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +0 -94
- data/spec/rspec/mocks/instance_method_stasher_spec.rb +0 -74
- data/spec/rspec/mocks/matchers/have_received_spec.rb +0 -347
- data/spec/rspec/mocks/matchers/receive_message_chain_spec.rb +0 -198
- data/spec/rspec/mocks/matchers/receive_messages_spec.rb +0 -140
- data/spec/rspec/mocks/matchers/receive_spec.rb +0 -418
- data/spec/rspec/mocks/method_signature_verifier_spec.rb +0 -272
- data/spec/rspec/mocks/methods_spec.rb +0 -26
- data/spec/rspec/mocks/mock_expectation_error_spec.rb +0 -22
- data/spec/rspec/mocks/mock_ordering_spec.rb +0 -114
- data/spec/rspec/mocks/multiple_return_value_spec.rb +0 -132
- data/spec/rspec/mocks/mutate_const_spec.rb +0 -542
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +0 -52
- data/spec/rspec/mocks/null_object_mock_spec.rb +0 -133
- data/spec/rspec/mocks/once_counts_spec.rb +0 -52
- data/spec/rspec/mocks/options_hash_spec.rb +0 -35
- data/spec/rspec/mocks/order_group_spec.rb +0 -27
- data/spec/rspec/mocks/partial_double_spec.rb +0 -308
- data/spec/rspec/mocks/partial_double_using_mocks_directly_spec.rb +0 -95
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +0 -145
- data/spec/rspec/mocks/precise_counts_spec.rb +0 -68
- data/spec/rspec/mocks/record_messages_spec.rb +0 -26
- data/spec/rspec/mocks/serialization_spec.rb +0 -90
- data/spec/rspec/mocks/space_spec.rb +0 -244
- data/spec/rspec/mocks/stash_spec.rb +0 -46
- data/spec/rspec/mocks/stub_chain_spec.rb +0 -168
- data/spec/rspec/mocks/stub_implementation_spec.rb +0 -81
- data/spec/rspec/mocks/stub_spec.rb +0 -327
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +0 -58
- data/spec/rspec/mocks/syntax_agnostic_message_matchers_spec.rb +0 -101
- data/spec/rspec/mocks/syntax_spec.rb +0 -19
- data/spec/rspec/mocks/test_double_spec.rb +0 -49
- data/spec/rspec/mocks/to_ary_spec.rb +0 -54
- data/spec/rspec/mocks/twice_counts_spec.rb +0 -66
- data/spec/rspec/mocks/verifying_double_spec.rb +0 -590
- data/spec/rspec/mocks/verifying_message_expecation_spec.rb +0 -69
- data/spec/rspec/mocks_spec.rb +0 -192
- data/spec/spec_helper.rb +0 -121
@@ -1,48 +0,0 @@
|
|
1
|
-
Feature: stub with substitute implementation
|
2
|
-
|
3
|
-
You can stub an implementation of a method (a.k.a. fake) by passing a block
|
4
|
-
to the `stub` method.
|
5
|
-
|
6
|
-
Scenario: stub implementation using `expect` syntax
|
7
|
-
Given a file named "stub_implementation_spec.rb" with:
|
8
|
-
"""ruby
|
9
|
-
describe "a stubbed implementation" do
|
10
|
-
it "works" do
|
11
|
-
object = Object.new
|
12
|
-
allow(object).to receive(:foo) do |arg|
|
13
|
-
if arg == :this
|
14
|
-
"got this"
|
15
|
-
elsif arg == :that
|
16
|
-
"got that"
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
expect(object.foo(:this)).to eq("got this")
|
21
|
-
expect(object.foo(:that)).to eq("got that")
|
22
|
-
end
|
23
|
-
end
|
24
|
-
"""
|
25
|
-
When I run `rspec stub_implementation_spec.rb`
|
26
|
-
Then the output should contain "1 example, 0 failures"
|
27
|
-
|
28
|
-
Scenario: stub implementation using `should` syntax
|
29
|
-
Given a file named "stub_implementation_spec.rb" with:
|
30
|
-
"""ruby
|
31
|
-
describe "a stubbed implementation" do
|
32
|
-
it "works" do
|
33
|
-
object = Object.new
|
34
|
-
object.stub(:foo) do |arg|
|
35
|
-
if arg == :this
|
36
|
-
"got this"
|
37
|
-
elsif arg == :that
|
38
|
-
"got that"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
expect(object.foo(:this)).to eq("got this")
|
43
|
-
expect(object.foo(:that)).to eq("got that")
|
44
|
-
end
|
45
|
-
end
|
46
|
-
"""
|
47
|
-
When I run `rspec stub_implementation_spec.rb`
|
48
|
-
Then the output should contain "1 example, 0 failures"
|
@@ -1,51 +0,0 @@
|
|
1
|
-
Feature: double handling to_ary
|
2
|
-
|
3
|
-
Ruby implicitly sends `to_ary` to all of the objects in an `Array` when the
|
4
|
-
array receives `flatten`:
|
5
|
-
|
6
|
-
[obj].flatten # => obj.to_ary
|
7
|
-
|
8
|
-
If `to_ary` raises a `NoMethodError`, Ruby sees that the object can not be coerced
|
9
|
-
into an array and moves on to the next object.
|
10
|
-
|
11
|
-
To support this, an RSpec double will raise a NoMethodError when it receives
|
12
|
-
`to_ary` _even if it is set `as_null_object`_, unless `to_ary` is explicitly
|
13
|
-
stubbed.
|
14
|
-
|
15
|
-
Scenario: double receiving to_ary
|
16
|
-
Given a file named "example.rb" with:
|
17
|
-
"""ruby
|
18
|
-
describe "#to_ary" do
|
19
|
-
shared_examples "to_ary" do
|
20
|
-
it "can be overridden with a stub" do
|
21
|
-
allow(obj).to receive(:to_ary) { :non_nil_value }
|
22
|
-
expect(obj.to_ary).to be(:non_nil_value)
|
23
|
-
end
|
24
|
-
|
25
|
-
it "supports Array#flatten" do
|
26
|
-
obj = double('foo')
|
27
|
-
expect([obj].flatten).to eq([obj])
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context "sent to a double as_null_object" do
|
32
|
-
let(:obj) { double('obj').as_null_object }
|
33
|
-
include_examples "to_ary"
|
34
|
-
|
35
|
-
it "returns nil" do
|
36
|
-
expect( obj.to_ary ).to eq nil
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context "sent to a double without as_null_object" do
|
41
|
-
let(:obj) { double('obj') }
|
42
|
-
include_examples "to_ary"
|
43
|
-
|
44
|
-
it "raises a NoMethodError" do
|
45
|
-
expect { obj.to_ary }.to raise_error(NoMethodError)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
"""
|
50
|
-
When I run `rspec example.rb`
|
51
|
-
Then the examples should all pass
|
@@ -1,82 +0,0 @@
|
|
1
|
-
## Mutating Constants
|
2
|
-
|
3
|
-
### Stubbing
|
4
|
-
|
5
|
-
Support is provided for stubbing constants. Like with method stubs, the
|
6
|
-
stubbed constants will be restored to their original state when an
|
7
|
-
example completes.
|
8
|
-
|
9
|
-
``` ruby
|
10
|
-
stub_const("Foo", fake_foo)
|
11
|
-
Foo # => fake_foo
|
12
|
-
```
|
13
|
-
|
14
|
-
Stubbed constant names must be fully qualified; the current module
|
15
|
-
nesting is not considered.
|
16
|
-
|
17
|
-
``` ruby
|
18
|
-
module MyGem
|
19
|
-
class SomeClass; end
|
20
|
-
end
|
21
|
-
|
22
|
-
module MyGem
|
23
|
-
describe "Something" do
|
24
|
-
let(:fake_class) { Class.new }
|
25
|
-
|
26
|
-
it "accidentally stubs the wrong constant" do
|
27
|
-
# this stubs ::SomeClass (in the top-level namespace),
|
28
|
-
# not MyGem::SomeClass like you probably mean.
|
29
|
-
stub_const("SomeClass", fake_class)
|
30
|
-
end
|
31
|
-
|
32
|
-
it "stubs the right constant" do
|
33
|
-
stub_const("MyGem::SomeClass", fake_class)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
```
|
38
|
-
|
39
|
-
When you stub a constant that is a module or a class, nested
|
40
|
-
constants on the original module or class are not transferred
|
41
|
-
by default, but you can use the `:transfer_nested_constants`
|
42
|
-
option to tell rspec-mocks to transfer them:
|
43
|
-
|
44
|
-
``` ruby
|
45
|
-
class CardDeck
|
46
|
-
SUITS = [:Spades, :Diamonds, :Clubs, :Hearts]
|
47
|
-
NUM_CARDS = 52
|
48
|
-
end
|
49
|
-
|
50
|
-
fake_class = Class.new
|
51
|
-
stub_const("CardDeck", fake_class)
|
52
|
-
CardDeck # => fake_class
|
53
|
-
CardDeck::SUITS # => raises uninitialized constant error
|
54
|
-
CardDeck::NUM_CARDS # => raises uninitialized constant error
|
55
|
-
|
56
|
-
stub_const("CardDeck", fake_class, :transfer_nested_constants => true)
|
57
|
-
CardDeck::SUITS # => [:Spades, :Diamonds, :Clubs, :Hearts]
|
58
|
-
CardDeck::NUM_CARDS # => 52
|
59
|
-
|
60
|
-
stub_const("CardDeck", fake_class, :transfer_nested_constants => [:SUITS])
|
61
|
-
CardDeck::SUITS # => [:Spades, :Diamonds, :Clubs, :Hearts]
|
62
|
-
CardDeck::NUM_CARDS # => raises uninitialized constant error
|
63
|
-
```
|
64
|
-
|
65
|
-
### Hiding
|
66
|
-
|
67
|
-
Support is also provided for hiding constants. Hiding a constant temporarily
|
68
|
-
removes it; it is restored to its original value after the test completes.
|
69
|
-
|
70
|
-
```ruby
|
71
|
-
FOO = 42
|
72
|
-
hide_const("FOO")
|
73
|
-
FOO => NameError: uninitialized constant FOO
|
74
|
-
```
|
75
|
-
|
76
|
-
Like stubbed constants, names must be fully qualified.
|
77
|
-
|
78
|
-
Hiding constants that are already undefined has no effect.
|
79
|
-
|
80
|
-
```ruby
|
81
|
-
hide_const("NO_OP")
|
82
|
-
```
|
@@ -1,64 +0,0 @@
|
|
1
|
-
Feature: Hide Defined Constant
|
2
|
-
|
3
|
-
Use `hide_const` to remove a constant for the duration of a test.
|
4
|
-
|
5
|
-
Scenario: Hide top-level constant
|
6
|
-
Given a file named "hide_const_spec.rb" with:
|
7
|
-
"""ruby
|
8
|
-
FOO = 7
|
9
|
-
|
10
|
-
describe "hiding FOO" do
|
11
|
-
it "can hide FOO" do
|
12
|
-
hide_const("FOO")
|
13
|
-
expect { FOO }.to raise_error(NameError)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "restores the hidden constant when the example completes" do
|
17
|
-
expect(FOO).to eq(7)
|
18
|
-
end
|
19
|
-
end
|
20
|
-
"""
|
21
|
-
When I run `rspec hide_const_spec.rb`
|
22
|
-
Then the examples should all pass
|
23
|
-
|
24
|
-
Scenario: Hide nested constant
|
25
|
-
Given a file named "hide_const_spec.rb" with:
|
26
|
-
"""ruby
|
27
|
-
module MyGem
|
28
|
-
class SomeClass
|
29
|
-
FOO = 7
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
module MyGem
|
34
|
-
describe SomeClass do
|
35
|
-
it "hides the nested constant when it is fully qualified" do
|
36
|
-
hide_const("MyGem::SomeClass::FOO")
|
37
|
-
expect { SomeClass::FOO }.to raise_error(NameError)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "restores the hidden constant when the example completes" do
|
41
|
-
expect(MyGem::SomeClass::FOO).to eq(7)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
"""
|
46
|
-
When I run `rspec hide_const_spec.rb`
|
47
|
-
Then the examples should all pass
|
48
|
-
|
49
|
-
Scenario: Hiding undefined constant
|
50
|
-
Given a file named "hide_const_spec.rb" with:
|
51
|
-
"""
|
52
|
-
describe "hiding UNDEFINED_CONSTANT" do
|
53
|
-
it "has no effect" do
|
54
|
-
hide_const("UNDEFINED_CONSTANT")
|
55
|
-
expect { UNDEFINED_CONSTANT }.to raise_error(NameError)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "is still undefined after the example completes" do
|
59
|
-
expect { UNDEFINED_CONSTANT }.to raise_error(NameError)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
"""
|
63
|
-
When I run `rspec hide_const_spec.rb`
|
64
|
-
Then the examples should all pass
|
@@ -1,78 +0,0 @@
|
|
1
|
-
Feature: Stub Defined Constant
|
2
|
-
|
3
|
-
Use `stub_const` to stub constants. When the constant is already defined,
|
4
|
-
the stubbed value will replace the original value for the duration of the
|
5
|
-
example.
|
6
|
-
|
7
|
-
Scenario: Stub top-level constant
|
8
|
-
Given a file named "stub_const_spec.rb" with:
|
9
|
-
"""ruby
|
10
|
-
FOO = 7
|
11
|
-
|
12
|
-
describe "stubbing FOO" do
|
13
|
-
it "can stub FOO with a different value" do
|
14
|
-
stub_const("FOO", 5)
|
15
|
-
expect(FOO).to eq(5)
|
16
|
-
end
|
17
|
-
|
18
|
-
it "restores the stubbed constant when the example completes" do
|
19
|
-
expect(FOO).to eq(7)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
"""
|
23
|
-
When I run `rspec stub_const_spec.rb`
|
24
|
-
Then the examples should all pass
|
25
|
-
|
26
|
-
Scenario: Stub nested constant
|
27
|
-
Given a file named "stub_const_spec.rb" with:
|
28
|
-
"""ruby
|
29
|
-
module MyGem
|
30
|
-
class SomeClass
|
31
|
-
FOO = 7
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
module MyGem
|
36
|
-
describe SomeClass do
|
37
|
-
it "stubs the nested constant when it is fully qualified" do
|
38
|
-
stub_const("MyGem::SomeClass::FOO", 5)
|
39
|
-
expect(SomeClass::FOO).to eq(5)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
"""
|
44
|
-
When I run `rspec stub_const_spec.rb`
|
45
|
-
Then the examples should all pass
|
46
|
-
|
47
|
-
Scenario: Transfer nested constants
|
48
|
-
Given a file named "stub_const_spec.rb" with:
|
49
|
-
"""ruby
|
50
|
-
module MyGem
|
51
|
-
class SomeClass
|
52
|
-
FOO = 7
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
module MyGem
|
57
|
-
describe SomeClass do
|
58
|
-
let(:fake_class) { Class.new }
|
59
|
-
|
60
|
-
it "does not transfer nested constants by default" do
|
61
|
-
stub_const("MyGem::SomeClass", fake_class)
|
62
|
-
expect { SomeClass::FOO }.to raise_error(NameError)
|
63
|
-
end
|
64
|
-
|
65
|
-
it "transfers nested constants when using :transfer_nested_constants => true" do
|
66
|
-
stub_const("MyGem::SomeClass", fake_class, :transfer_nested_constants => true)
|
67
|
-
expect(SomeClass::FOO).to eq(7)
|
68
|
-
end
|
69
|
-
|
70
|
-
it "can specify a list of nested constants to transfer" do
|
71
|
-
stub_const("MyGem::SomeClass", fake_class, :transfer_nested_constants => [:FOO])
|
72
|
-
expect(SomeClass::FOO).to eq(7)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
"""
|
77
|
-
When I run `rspec stub_const_spec.rb`
|
78
|
-
Then the examples should all pass
|
@@ -1,50 +0,0 @@
|
|
1
|
-
Feature: Stub Undefined Constant
|
2
|
-
|
3
|
-
Use `stub_const` to stub constants. When the constant is not already defined,
|
4
|
-
all the necessary intermediary modules will be dynamically created. When the
|
5
|
-
example completes, the intermediary module constants will be removed to return
|
6
|
-
the constant state to how it started.
|
7
|
-
|
8
|
-
Scenario: Stub top-level constant
|
9
|
-
Given a file named "stub_const_spec.rb" with:
|
10
|
-
"""ruby
|
11
|
-
describe "stubbing FOO" do
|
12
|
-
it "can stub undefined constant FOO" do
|
13
|
-
stub_const("FOO", 5)
|
14
|
-
expect(FOO).to eq(5)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "undefines the constant when the example completes" do
|
18
|
-
expect { FOO }.to raise_error(NameError)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
"""
|
22
|
-
When I run `rspec stub_const_spec.rb`
|
23
|
-
Then the examples should all pass
|
24
|
-
|
25
|
-
Scenario: Stub nested constant
|
26
|
-
Given a file named "stub_const_spec.rb" with:
|
27
|
-
"""ruby
|
28
|
-
module MyGem
|
29
|
-
class SomeClass
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
module MyGem
|
34
|
-
describe SomeClass do
|
35
|
-
it "can stub an arbitrarily deep constant that is undefined" do
|
36
|
-
expect(defined?(SomeClass::A)).to be_falsey
|
37
|
-
stub_const("MyGem::SomeClass::A::B::C", 3)
|
38
|
-
expect(SomeClass::A::B::C).to eq(3)
|
39
|
-
expect(SomeClass::A).to be_a(Module)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'undefines the intermediary constants that were dynamically created' do
|
43
|
-
expect(defined?(SomeClass)).to be_truthy
|
44
|
-
expect(defined?(SomeClass::A)).to be_falsey
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
"""
|
49
|
-
When I run `rspec stub_const_spec.rb`
|
50
|
-
Then the examples should all pass
|
@@ -1,33 +0,0 @@
|
|
1
|
-
Feature: standalone
|
2
|
-
|
3
|
-
require "rspec/mocks/standalone" to expose the mock framework
|
4
|
-
outside the RSpec environment. This is especially useful for
|
5
|
-
exploring rspec-mocks in irb.
|
6
|
-
|
7
|
-
Scenario: method stub outside rspec
|
8
|
-
Given a file named "example.rb" with:
|
9
|
-
"""ruby
|
10
|
-
require "rspec/mocks/standalone"
|
11
|
-
|
12
|
-
greeter = double("greeter")
|
13
|
-
allow(greeter).to receive(:say_hi) { "Hello!" }
|
14
|
-
puts greeter.say_hi
|
15
|
-
"""
|
16
|
-
When I run `ruby example.rb`
|
17
|
-
Then the output should contain "Hello!"
|
18
|
-
|
19
|
-
Scenario: message expectation outside rspec
|
20
|
-
Given a file named "example.rb" with:
|
21
|
-
"""ruby
|
22
|
-
require "rspec/mocks/standalone"
|
23
|
-
|
24
|
-
greeter = double("greeter")
|
25
|
-
expect(greeter).to receive(:say_hi)
|
26
|
-
|
27
|
-
RSpec::Mocks.verify
|
28
|
-
"""
|
29
|
-
When I run `ruby example.rb`
|
30
|
-
Then the output should contain "RSpec::Mocks::MockExpectationError"
|
31
|
-
And the output should contain "say_hi(any args)"
|
32
|
-
And the output should contain "expected: 1 time"
|
33
|
-
And the output should contain "received: 0 times"
|
@@ -1,34 +0,0 @@
|
|
1
|
-
Feature: Spy on a stubbed method on a partial mock
|
2
|
-
|
3
|
-
You can also use `have_received` to verify that a stubbed method was invoked
|
4
|
-
on a partial mock.
|
5
|
-
|
6
|
-
Scenario: verify a stubbed method
|
7
|
-
Given a file named "verified_spy_spec.rb" with:
|
8
|
-
"""ruby
|
9
|
-
describe "have_received" do
|
10
|
-
it "passes when the expectation is met" do
|
11
|
-
invitation = Object.new
|
12
|
-
invitation.stub(:deliver => true)
|
13
|
-
invitation.deliver
|
14
|
-
expect(invitation).to have_received(:deliver)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
"""
|
18
|
-
When I run `rspec verified_spy_spec.rb`
|
19
|
-
Then the examples should all pass
|
20
|
-
|
21
|
-
Scenario: fail to verify a stubbed method
|
22
|
-
Given a file named "failed_spy_spec.rb" with:
|
23
|
-
"""ruby
|
24
|
-
describe "have_received" do
|
25
|
-
it "fails when the expectation is not met" do
|
26
|
-
invitation = Object.new
|
27
|
-
invitation.stub(:deliver => true)
|
28
|
-
expect(invitation).to have_received(:deliver)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
"""
|
32
|
-
When I run `rspec failed_spy_spec.rb`
|
33
|
-
Then the output should contain "expected: 1 time"
|
34
|
-
And the output should contain "received: 0 times"
|