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,76 +0,0 @@
|
|
1
|
-
Feature: Spy on a stubbed method on a pure mock
|
2
|
-
|
3
|
-
You can use `have_received` to verify that a stubbed method was invoked,
|
4
|
-
rather than setting an expectation for it to be invoked beforehand.
|
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 = double('invitation', :deliver => true)
|
12
|
-
invitation.deliver
|
13
|
-
expect(invitation).to have_received(:deliver)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
"""
|
17
|
-
When I run `rspec verified_spy_spec.rb`
|
18
|
-
Then the examples should all pass
|
19
|
-
|
20
|
-
Scenario: verify a stubbed method with message expectations
|
21
|
-
Given a file named "verified_message_expectations_spec.rb" with:
|
22
|
-
"""ruby
|
23
|
-
describe "have_received" do
|
24
|
-
it "passes when the expectation is met" do
|
25
|
-
invitation = double('invitation', :deliver => true)
|
26
|
-
2.times { invitation.deliver(:expected, :arguments) }
|
27
|
-
expect(invitation).to have_received(:deliver).
|
28
|
-
with(:expected, :arguments).
|
29
|
-
twice
|
30
|
-
end
|
31
|
-
end
|
32
|
-
"""
|
33
|
-
When I run `rspec verified_message_expectations_spec.rb`
|
34
|
-
Then the examples should all pass
|
35
|
-
|
36
|
-
Scenario: fail to verify a stubbed method
|
37
|
-
Given a file named "failed_spy_spec.rb" with:
|
38
|
-
"""ruby
|
39
|
-
describe "have_received" do
|
40
|
-
it "fails when the expectation is not met" do
|
41
|
-
invitation = double('invitation', :deliver => true)
|
42
|
-
expect(invitation).to have_received(:deliver)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
"""
|
46
|
-
When I run `rspec failed_spy_spec.rb`
|
47
|
-
Then the output should contain "expected: 1 time"
|
48
|
-
And the output should contain "received: 0 times"
|
49
|
-
|
50
|
-
Scenario: fail to verify message expectations
|
51
|
-
Given a file named "failed_message_expectations_spec.rb" with:
|
52
|
-
"""ruby
|
53
|
-
describe "have_received" do
|
54
|
-
it "fails when the arguments are different" do
|
55
|
-
invitation = double('invitation', :deliver => true)
|
56
|
-
invitation.deliver(:unexpected)
|
57
|
-
expect(invitation).to have_received(:deliver).with(:expected, :arguments)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
"""
|
61
|
-
When I run `rspec failed_message_expectations_spec.rb`
|
62
|
-
Then the output should contain "expected: (:expected, :arguments)"
|
63
|
-
And the output should contain "got: (:unexpected)"
|
64
|
-
|
65
|
-
Scenario: generate a spy message
|
66
|
-
Given a file named "spy_message_spec.rb" with:
|
67
|
-
"""ruby
|
68
|
-
describe "have_received" do
|
69
|
-
subject(:invitation) { double('invitation', :deliver => true) }
|
70
|
-
before { invitation.deliver }
|
71
|
-
|
72
|
-
it { should have_received(:deliver) }
|
73
|
-
end
|
74
|
-
"""
|
75
|
-
When I run `rspec --format documentation spy_message_spec.rb`
|
76
|
-
Then the output should contain "should have received deliver"
|
@@ -1,18 +0,0 @@
|
|
1
|
-
Feature: Spy on an unstubbed method
|
2
|
-
|
3
|
-
Using have_received on an unstubbed method will never pass, so rspec-mocks
|
4
|
-
issues a helpful error message.
|
5
|
-
|
6
|
-
Scenario: fail to verify a stubbed method
|
7
|
-
Given a file named "failed_spy_spec.rb" with:
|
8
|
-
"""ruby
|
9
|
-
describe "have_received" do
|
10
|
-
it "raises a helpful error for unstubbed methods" do
|
11
|
-
object = Object.new
|
12
|
-
object.object_id
|
13
|
-
expect(object).to have_received(:object_id)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
"""
|
17
|
-
When I run `rspec failed_spy_spec.rb`
|
18
|
-
Then the output should contain "that method has not been stubbed"
|
@@ -1,11 +0,0 @@
|
|
1
|
-
Then /^the example(?:s)? should(?: all)? pass$/ do
|
2
|
-
step %q{the output should contain "0 failures"}
|
3
|
-
step %q{the exit status should be 0}
|
4
|
-
end
|
5
|
-
|
6
|
-
# Useful for when the output is slightly different on different versions of ruby
|
7
|
-
Then /^the output should contain "([^"]*)" or "([^"]*)"$/ do |string1, string2|
|
8
|
-
unless [string1, string2].any? { |s| all_output =~ regexp(s) }
|
9
|
-
fail %Q{Neither "#{string1}" or "#{string2}" were found in:\n#{all_output}}
|
10
|
-
end
|
11
|
-
end
|
data/features/support/env.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'aruba/cucumber'
|
2
|
-
require 'rspec/expectations'
|
3
|
-
|
4
|
-
Before do
|
5
|
-
if RUBY_PLATFORM =~ /java/ || defined?(Rubinius)
|
6
|
-
@aruba_timeout_seconds = 60
|
7
|
-
else
|
8
|
-
@aruba_timeout_seconds = 5
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
Aruba.configure do |config|
|
13
|
-
config.before_cmd do |cmd|
|
14
|
-
set_env('JRUBY_OPTS', "-X-C #{ENV['JRUBY_OPTS']}") # disable JIT since these processes are so short lived
|
15
|
-
end
|
16
|
-
end if RUBY_PLATFORM == 'java'
|
17
|
-
|
18
|
-
Aruba.configure do |config|
|
19
|
-
config.before_cmd do |cmd|
|
20
|
-
set_env('RBXOPT', "-Xint=true #{ENV['RBXOPT']}") # disable JIT since these processes are so short lived
|
21
|
-
end
|
22
|
-
end if defined?(Rubinius)
|
@@ -1,54 +0,0 @@
|
|
1
|
-
Feature: Test::Unit integration
|
2
|
-
|
3
|
-
rspec-mocks is a stand-alone gem that can be used without the rest of
|
4
|
-
RSpec. If you like the way Test::Unit (or MiniTest) organizes tests, but
|
5
|
-
prefer RSpec's approach to mocking/stubbing/doubles etc, you can have both.
|
6
|
-
|
7
|
-
The one downside is that failures are reported as errors with MiniTest.
|
8
|
-
|
9
|
-
Scenario: use rspec/mocks with Test::Unit
|
10
|
-
Given a file named "rspec_mocks_test.rb" with:
|
11
|
-
"""ruby
|
12
|
-
require 'test/unit'
|
13
|
-
require 'rspec/mocks'
|
14
|
-
|
15
|
-
class RSpecMocksTest < Test::Unit::TestCase
|
16
|
-
include RSpec::Mocks::ExampleMethods
|
17
|
-
|
18
|
-
def setup
|
19
|
-
RSpec::Mocks.setup
|
20
|
-
end
|
21
|
-
|
22
|
-
def teardown
|
23
|
-
RSpec::Mocks.verify
|
24
|
-
ensure
|
25
|
-
RSpec::Mocks.teardown
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_passing_positive_expectation
|
29
|
-
obj = Object.new
|
30
|
-
expect(obj).to receive(:message)
|
31
|
-
obj.message
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_failing_positive_expectation
|
35
|
-
obj = Object.new
|
36
|
-
expect(obj).to receive(:message)
|
37
|
-
obj.message
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_passing_negative_expectation
|
41
|
-
obj = Object.new
|
42
|
-
expect(obj).to_not receive(:message)
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_failing_negative_expectation
|
46
|
-
obj = Object.new
|
47
|
-
expect(obj).to_not receive(:message)
|
48
|
-
obj.message
|
49
|
-
end
|
50
|
-
end
|
51
|
-
"""
|
52
|
-
When I run `ruby rspec_mocks_test.rb`
|
53
|
-
Then the output should contain "4 tests, 0 assertions, 0 failures, 1 errors" or "4 tests, 0 assertions, 1 failures, 0 errors"
|
54
|
-
And the output should contain "expected: 0 times with any arguments"
|
@@ -1,17 +0,0 @@
|
|
1
|
-
## Verifying Doubles
|
2
|
-
|
3
|
-
Verifying doubles are a stricter alternative to normal doubles that provide
|
4
|
-
guarantees about what is being verified. When using verifying doubles, RSpec
|
5
|
-
will check that the methods being stubbed are actually present on the
|
6
|
-
underlying object if it is available. Prefer using veryifing doubles over
|
7
|
-
normal doubles.
|
8
|
-
|
9
|
-
No checking will happen if the underlying object or class is not defined, but
|
10
|
-
when run with it present (either as a full spec run or by explicitly preloading
|
11
|
-
collaborators) a failure will be triggered if an invalid method is being
|
12
|
-
stubbed or a method is called with an invalid number of arguments.
|
13
|
-
|
14
|
-
This dual approach allows you to move very quickly and test components in
|
15
|
-
isolation, while giving you confidence that your doubles are not a complete
|
16
|
-
fiction. Testing in isolation is optional but recommend for classes that do not
|
17
|
-
depend on third-party components.
|
@@ -1,73 +0,0 @@
|
|
1
|
-
Feature: Using a class double
|
2
|
-
|
3
|
-
`class_double` is provided as a complement to `instance_double`, with the
|
4
|
-
difference that it verifies _class_ methods on the given class rather than
|
5
|
-
instance methods.
|
6
|
-
|
7
|
-
In addition, it also provides a convenience method `as_stubbed_const` to
|
8
|
-
replace concrete classes with the defined double. See [mutating
|
9
|
-
constants](../mutating-constants) for more details.
|
10
|
-
|
11
|
-
Note: `class_double` can be used for modules as well. We chose to stick with
|
12
|
-
the `class_double` terminology because the methods a `class_double` verifies
|
13
|
-
against are commonly called "class methods", not "module methods", even when
|
14
|
-
working with a module.
|
15
|
-
|
16
|
-
Background:
|
17
|
-
Given a file named "lib/user.rb" with:
|
18
|
-
"""ruby
|
19
|
-
class User
|
20
|
-
def suspend!
|
21
|
-
ConsoleNotifier.notify("suspended as")
|
22
|
-
end
|
23
|
-
end
|
24
|
-
"""
|
25
|
-
|
26
|
-
Given a file named "lib/console_notifier.rb" with:
|
27
|
-
"""ruby
|
28
|
-
class ConsoleNotifier
|
29
|
-
MAX_WIDTH = 80
|
30
|
-
|
31
|
-
def self.notify(message)
|
32
|
-
puts message
|
33
|
-
end
|
34
|
-
end
|
35
|
-
"""
|
36
|
-
|
37
|
-
Given a file named "spec/user_spec.rb" with:
|
38
|
-
"""ruby
|
39
|
-
require 'user'
|
40
|
-
require 'console_notifier'
|
41
|
-
|
42
|
-
describe User, '#suspend!' do
|
43
|
-
it 'notifies the console' do
|
44
|
-
notifier = class_double("ConsoleNotifier").
|
45
|
-
as_stubbed_const(:transfer_nested_constants => true)
|
46
|
-
|
47
|
-
expect(notifier).to receive(:notify).with("suspended as")
|
48
|
-
expect(ConsoleNotifier::MAX_WIDTH).to eq(80)
|
49
|
-
|
50
|
-
user = User.new
|
51
|
-
user.suspend!
|
52
|
-
end
|
53
|
-
end
|
54
|
-
"""
|
55
|
-
|
56
|
-
Scenario: replacing existing constants
|
57
|
-
When I run `rspec spec/user_spec.rb`
|
58
|
-
Then the examples should all pass
|
59
|
-
|
60
|
-
Scenario: renaming `ConsoleNotifier.notify` to `send_notification`
|
61
|
-
Given a file named "lib/console_notifier.rb" with:
|
62
|
-
"""ruby
|
63
|
-
class ConsoleNotifier
|
64
|
-
MAX_WIDTH = 80
|
65
|
-
|
66
|
-
def self.send_notification(message)
|
67
|
-
puts message
|
68
|
-
end
|
69
|
-
end
|
70
|
-
"""
|
71
|
-
When I run `rspec spec/user_spec.rb`
|
72
|
-
Then the output should contain "1 example, 1 failure"
|
73
|
-
And the output should contain "ConsoleNotifier does not implement:"
|
@@ -1,71 +0,0 @@
|
|
1
|
-
Feature: Dynamic classes
|
2
|
-
|
3
|
-
Verifying instance doubles do not support methods which the class reports to
|
4
|
-
not exist since an actual instance of the class would be required to verify
|
5
|
-
against. This is commonly the case when `method_missing` is used.
|
6
|
-
`ActiveRecord` does this to define methods from database columns. If the
|
7
|
-
object has already been loaded you may consider using an `object_double`, but
|
8
|
-
that cannot work if you are testing in isolation.
|
9
|
-
|
10
|
-
These types of methods are supported at class level, since `respond_to?` can
|
11
|
-
be queried directly on the class.
|
12
|
-
|
13
|
-
|
14
|
-
Background:
|
15
|
-
Given a file named "lib/fake_active_record.rb" with:
|
16
|
-
"""ruby
|
17
|
-
class FakeActiveRecord
|
18
|
-
COLUMNS = %w[name email]
|
19
|
-
|
20
|
-
def respond_to_missing?(method_name)
|
21
|
-
COLUMNS.include?(method_name.to_s) || super
|
22
|
-
end
|
23
|
-
|
24
|
-
def method_missing(method_name, *args)
|
25
|
-
if respond_to?(method_name)
|
26
|
-
instance_variable_get("@#{method_name}")
|
27
|
-
else
|
28
|
-
super
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
"""
|
33
|
-
|
34
|
-
Given a file named "spec/user_spec.rb" with:
|
35
|
-
"""ruby
|
36
|
-
require 'user'
|
37
|
-
|
38
|
-
describe User do
|
39
|
-
it 'can be doubled' do
|
40
|
-
instance_double("User", :name => "Don")
|
41
|
-
end
|
42
|
-
end
|
43
|
-
"""
|
44
|
-
|
45
|
-
Scenario: fails with method missing
|
46
|
-
|
47
|
-
Given a file named "lib/user.rb" with:
|
48
|
-
"""ruby
|
49
|
-
require 'fake_active_record'
|
50
|
-
|
51
|
-
class User < FakeActiveRecord
|
52
|
-
end
|
53
|
-
"""
|
54
|
-
|
55
|
-
When I run `rspec spec/user_spec.rb`
|
56
|
-
Then the output should contain "1 example, 1 failure"
|
57
|
-
|
58
|
-
Scenario: workaround with explict definitions
|
59
|
-
|
60
|
-
Given a file named "lib/user.rb" with:
|
61
|
-
"""ruby
|
62
|
-
require 'fake_active_record'
|
63
|
-
|
64
|
-
class User < FakeActiveRecord
|
65
|
-
def name; super end
|
66
|
-
def email; super end
|
67
|
-
end
|
68
|
-
"""
|
69
|
-
|
70
|
-
When I run `rspec spec/user_spec.rb`
|
71
|
-
Then the examples should all pass
|
@@ -1,103 +0,0 @@
|
|
1
|
-
Feature: Using an instance double
|
2
|
-
|
3
|
-
An `instance_double` is the most common type of verifying double. It takes a
|
4
|
-
class name or object as its first argument, then verifies that any methods
|
5
|
-
being stubbed would be present on an _instance_ of that class. If any
|
6
|
-
argument matchers are specified, it also verifies that the number of
|
7
|
-
arguments is correct.
|
8
|
-
|
9
|
-
For methods handled by `method_missing`, see [dynamic
|
10
|
-
objects](./dynamic-objects).
|
11
|
-
|
12
|
-
Background:
|
13
|
-
Given a file named "app/models/user.rb" with:
|
14
|
-
"""ruby
|
15
|
-
class User < Struct.new(:notifier)
|
16
|
-
def suspend!
|
17
|
-
notifier.notify("suspended as")
|
18
|
-
end
|
19
|
-
end
|
20
|
-
"""
|
21
|
-
|
22
|
-
Given a file named "spec/unit_helper.rb" with:
|
23
|
-
"""ruby
|
24
|
-
$LOAD_PATH.unshift("app/models")
|
25
|
-
"""
|
26
|
-
|
27
|
-
Given a file named "spec/spec_helper.rb" with:
|
28
|
-
"""ruby
|
29
|
-
require 'unit_helper'
|
30
|
-
|
31
|
-
require 'user'
|
32
|
-
require 'console_notifier'
|
33
|
-
|
34
|
-
RSpec.configure do |config|
|
35
|
-
config.mock_with :rspec do |mocks|
|
36
|
-
|
37
|
-
# This option should be set when all dependencies are being loaded
|
38
|
-
# before a spec run, as is the case in a typical spec helper. It will
|
39
|
-
# cause any verifying double instantiation for a class that does not
|
40
|
-
# exist to raise, protecting against incorrectly spelt names.
|
41
|
-
mocks.verify_doubled_constant_names = true
|
42
|
-
|
43
|
-
end
|
44
|
-
end
|
45
|
-
"""
|
46
|
-
|
47
|
-
Given a file named "spec/unit/user_spec.rb" with:
|
48
|
-
"""ruby
|
49
|
-
require 'unit_helper'
|
50
|
-
|
51
|
-
require 'user'
|
52
|
-
|
53
|
-
describe User, '#suspend!' do
|
54
|
-
it 'notifies the console' do
|
55
|
-
notifier = instance_double("ConsoleNotifier")
|
56
|
-
|
57
|
-
expect(notifier).to receive(:notify).with("suspended as")
|
58
|
-
|
59
|
-
user = User.new(notifier)
|
60
|
-
user.suspend!
|
61
|
-
end
|
62
|
-
end
|
63
|
-
"""
|
64
|
-
|
65
|
-
Scenario: spec passes in isolation
|
66
|
-
When I run `rspec spec/unit/user_spec.rb`
|
67
|
-
Then the examples should all pass
|
68
|
-
|
69
|
-
Scenario: spec passes with dependencies loaded and method implemented
|
70
|
-
Given a file named "app/models/console_notifier.rb" with:
|
71
|
-
"""ruby
|
72
|
-
class ConsoleNotifier
|
73
|
-
def notify(msg)
|
74
|
-
puts message
|
75
|
-
end
|
76
|
-
end
|
77
|
-
"""
|
78
|
-
|
79
|
-
When I run `rspec -r./spec/spec_helper spec/unit/user_spec.rb`
|
80
|
-
Then the examples should all pass
|
81
|
-
|
82
|
-
Scenario: spec fails with dependencies loaded and method unimplemented
|
83
|
-
Given a file named "app/models/console_notifier.rb" with:
|
84
|
-
"""ruby
|
85
|
-
class ConsoleNotifier
|
86
|
-
end
|
87
|
-
"""
|
88
|
-
When I run `rspec -r./spec/spec_helper spec/unit/user_spec.rb`
|
89
|
-
Then the output should contain "1 example, 1 failure"
|
90
|
-
And the output should contain "ConsoleNotifier does not implement:"
|
91
|
-
|
92
|
-
Scenario: spec fails with dependencies loaded and incorrect arity
|
93
|
-
Given a file named "app/models/console_notifier.rb" with:
|
94
|
-
"""ruby
|
95
|
-
class ConsoleNotifier
|
96
|
-
def notify(msg, color)
|
97
|
-
puts color + message
|
98
|
-
end
|
99
|
-
end
|
100
|
-
"""
|
101
|
-
When I run `rspec -r./spec/spec_helper spec/unit/user_spec.rb`
|
102
|
-
Then the output should contain "1 example, 1 failure"
|
103
|
-
And the output should contain "Wrong number of arguments."
|