rspec-core 2.0.0.a1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +7 -0
- data/.treasure_map.rb +23 -0
- data/License.txt +22 -0
- data/README.markdown +9 -0
- data/Rakefile +76 -0
- data/TODO.markdown +16 -0
- data/VERSION +1 -0
- data/VERSION.yml +5 -0
- data/bin/rspec +12 -0
- data/cucumber.yml +2 -0
- data/example_specs/failing/README.txt +7 -0
- data/example_specs/failing/diffing_spec.rb +36 -0
- data/example_specs/failing/failing_implicit_docstrings_example.rb +19 -0
- data/example_specs/failing/failure_in_after.rb +10 -0
- data/example_specs/failing/failure_in_before.rb +10 -0
- data/example_specs/failing/mocking_example.rb +40 -0
- data/example_specs/failing/mocking_with_flexmock.rb +26 -0
- data/example_specs/failing/mocking_with_mocha.rb +25 -0
- data/example_specs/failing/mocking_with_rr.rb +27 -0
- data/example_specs/failing/partial_mock_example.rb +20 -0
- data/example_specs/failing/pending_example.rb +9 -0
- data/example_specs/failing/predicate_example.rb +34 -0
- data/example_specs/failing/raising_example.rb +47 -0
- data/example_specs/failing/spec_helper.rb +3 -0
- data/example_specs/failing/syntax_error_example.rb +7 -0
- data/example_specs/failing/team_spec.rb +44 -0
- data/example_specs/failing/timeout_behaviour.rb +7 -0
- data/example_specs/passing/custom_formatter.rb +12 -0
- data/example_specs/passing/custom_matchers.rb +54 -0
- data/example_specs/passing/dynamic_spec.rb +9 -0
- data/example_specs/passing/file_accessor.rb +19 -0
- data/example_specs/passing/file_accessor_spec.rb +38 -0
- data/example_specs/passing/filtered_formatter.rb +18 -0
- data/example_specs/passing/filtered_formatter_example.rb +31 -0
- data/example_specs/passing/greeter_spec.rb +31 -0
- data/example_specs/passing/helper_method_example.rb +14 -0
- data/example_specs/passing/implicit_docstrings_example.rb +18 -0
- data/example_specs/passing/io_processor.rb +8 -0
- data/example_specs/passing/io_processor_spec.rb +21 -0
- data/example_specs/passing/mocking_example.rb +27 -0
- data/example_specs/passing/multi_threaded_example_group_runner.rb +26 -0
- data/example_specs/passing/nested_classes_example.rb +36 -0
- data/example_specs/passing/options_example.rb +31 -0
- data/example_specs/passing/options_formatter.rb +20 -0
- data/example_specs/passing/partial_mock_example.rb +29 -0
- data/example_specs/passing/pending_example.rb +20 -0
- data/example_specs/passing/predicate_example.rb +27 -0
- data/example_specs/passing/shared_example_group_example.rb +81 -0
- data/example_specs/passing/shared_stack_examples.rb +36 -0
- data/example_specs/passing/simple_matcher_example.rb +31 -0
- data/example_specs/passing/spec_helper.rb +14 -0
- data/example_specs/passing/stack.rb +36 -0
- data/example_specs/passing/stack_spec.rb +64 -0
- data/example_specs/passing/stack_spec_with_nested_example_groups.rb +67 -0
- data/example_specs/passing/stubbing_example.rb +69 -0
- data/example_specs/passing/yielding_example.rb +33 -0
- data/example_specs/ruby1.9.compatibility/access_to_constants_spec.rb +85 -0
- data/features-pending/command_line/line_number_option.feature +56 -0
- data/features-pending/command_line/line_number_option_with_example_with_no_name.feature +22 -0
- data/features-pending/example_groups/example_group_with_should_methods.feature +29 -0
- data/features-pending/example_groups/implicit_docstrings.feature +59 -0
- data/features-pending/example_groups/nested_groups.feature +32 -0
- data/features-pending/expectations/expect_change.feature +65 -0
- data/features-pending/expectations/expect_error.feature +44 -0
- data/features-pending/extensions/custom_example_group.feature +19 -0
- data/features-pending/formatters/custom_formatter.feature +30 -0
- data/features-pending/heckle/heckle.feature +56 -0
- data/features-pending/interop/examples_and_tests_together.feature +80 -0
- data/features-pending/interop/rspec_output.feature +25 -0
- data/features-pending/interop/test_but_not_test_unit.feature +26 -0
- data/features-pending/interop/test_case_with_should_methods.feature +46 -0
- data/features-pending/matchers/define_diffable_matcher.feature +26 -0
- data/features-pending/matchers/define_matcher.feature +179 -0
- data/features-pending/matchers/define_matcher_with_fluent_interface.feature +27 -0
- data/features-pending/mocks/mix_stubs_and_mocks.feature +22 -0
- data/features-pending/mocks/stub_implementation.feature +26 -0
- data/features-pending/pending/pending_examples.feature +81 -0
- data/features-pending/runner/specify_line_number.feature +32 -0
- data/features/before_and_after_blocks/before_and_after_blocks.feature +169 -0
- data/features/expectations/customized_message.feature +54 -0
- data/features/matchers/define_matcher_outside_rspec.feature +39 -0
- data/features/mock_framework_integration/use_flexmock.feature +23 -0
- data/features/mock_framework_integration/use_mocha.feature +23 -0
- data/features/mock_framework_integration/use_rr.feature +23 -0
- data/features/mock_framework_integration/use_rspec.feature +23 -0
- data/features/step_definitions/running_rspec_steps.rb +35 -0
- data/features/subject/explicit_subject.feature +31 -0
- data/features/subject/implicit_subject.feature +31 -0
- data/features/support/env.rb +82 -0
- data/features/support/matchers/smart_match.rb +14 -0
- data/lib/rspec/autorun.rb +2 -0
- data/lib/rspec/core.rb +38 -0
- data/lib/rspec/core/backward_compatibility.rb +9 -0
- data/lib/rspec/core/command_line_options.rb +60 -0
- data/lib/rspec/core/configuration.rb +222 -0
- data/lib/rspec/core/deprecation.rb +47 -0
- data/lib/rspec/core/example.rb +113 -0
- data/lib/rspec/core/example_group.rb +239 -0
- data/lib/rspec/core/example_group_subject.rb +77 -0
- data/lib/rspec/core/formatters.rb +16 -0
- data/lib/rspec/core/formatters/base_formatter.rb +123 -0
- data/lib/rspec/core/formatters/base_text_formatter.rb +139 -0
- data/lib/rspec/core/formatters/documentation_formatter.rb +84 -0
- data/lib/rspec/core/formatters/progress_formatter.rb +36 -0
- data/lib/rspec/core/kernel_extensions.rb +15 -0
- data/lib/rspec/core/mocking/with_absolutely_nothing.rb +13 -0
- data/lib/rspec/core/mocking/with_flexmock.rb +25 -0
- data/lib/rspec/core/mocking/with_mocha.rb +22 -0
- data/lib/rspec/core/mocking/with_rr.rb +26 -0
- data/lib/rspec/core/mocking/with_rspec.rb +21 -0
- data/lib/rspec/core/rake_task.rb +88 -0
- data/lib/rspec/core/runner.rb +66 -0
- data/lib/rspec/core/shared_behaviour.rb +41 -0
- data/lib/rspec/core/shared_behaviour_kernel_extensions.rb +31 -0
- data/lib/rspec/core/version.rb +16 -0
- data/lib/rspec/core/world.rb +105 -0
- data/rspec-core.gemspec +204 -0
- data/script/console +8 -0
- data/spec/resources/example_classes.rb +67 -0
- data/spec/rspec/core/command_line_options_spec.rb +63 -0
- data/spec/rspec/core/configuration_spec.rb +171 -0
- data/spec/rspec/core/example_group_spec.rb +351 -0
- data/spec/rspec/core/example_group_subject_spec.rb +67 -0
- data/spec/rspec/core/example_spec.rb +67 -0
- data/spec/rspec/core/formatters/base_formatter_spec.rb +105 -0
- data/spec/rspec/core/formatters/documentation_formatter_spec.rb +5 -0
- data/spec/rspec/core/formatters/progress_formatter_spec.rb +29 -0
- data/spec/rspec/core/kernel_extensions_spec.rb +13 -0
- data/spec/rspec/core/mocha_spec.rb +29 -0
- data/spec/rspec/core/resources/a_bar.rb +0 -0
- data/spec/rspec/core/resources/a_foo.rb +0 -0
- data/spec/rspec/core/resources/a_spec.rb +1 -0
- data/spec/rspec/core/resources/custom_example_group_runner.rb +14 -0
- data/spec/rspec/core/resources/example_classes.rb +67 -0
- data/spec/rspec/core/resources/utf8_encoded.rb +8 -0
- data/spec/rspec/core/runner_spec.rb +34 -0
- data/spec/rspec/core/shared_behaviour_spec.rb +185 -0
- data/spec/rspec/core/world_spec.rb +167 -0
- data/spec/rspec/core_spec.rb +35 -0
- data/spec/ruby_forker.rb +13 -0
- data/spec/spec_helper.rb +72 -0
- metadata +219 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
Feature: define matcher
|
2
|
+
|
3
|
+
In order to express my domain clearly in my code examples
|
4
|
+
As an RSpec user
|
5
|
+
I want to define matchers with fluent interfaces
|
6
|
+
|
7
|
+
Scenario: one additional method
|
8
|
+
Given a file named "between_spec.rb" with:
|
9
|
+
"""
|
10
|
+
Rspec::Matchers.define :be_bigger_than do |first|
|
11
|
+
def but_smaller_than(second)
|
12
|
+
@second = second
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
match do |actual|
|
17
|
+
(actual > first) && (actual < @second)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe 5 do
|
22
|
+
it { should be_bigger_than(4).but_smaller_than(6) }
|
23
|
+
end
|
24
|
+
"""
|
25
|
+
When I run "spec between_spec.rb --format specdoc"
|
26
|
+
Then the stdout should match "1 example, 0 failures"
|
27
|
+
And the stdout should match "should be bigger than 4"
|
@@ -0,0 +1,22 @@
|
|
1
|
+
Feature: Spec and test together
|
2
|
+
|
3
|
+
As an RSpec user
|
4
|
+
I want to use stubs and mocks together
|
5
|
+
|
6
|
+
Scenario: stub in before
|
7
|
+
Given a file named "stub_and_mocks_spec.rb" with:
|
8
|
+
"""
|
9
|
+
describe "a stub in before" do
|
10
|
+
before(:each) do
|
11
|
+
@messenger = mock('messenger').as_null_object
|
12
|
+
end
|
13
|
+
|
14
|
+
it "a" do
|
15
|
+
@messenger.should_receive(:foo).with('first')
|
16
|
+
@messenger.foo('second')
|
17
|
+
@messenger.foo('third')
|
18
|
+
end
|
19
|
+
end
|
20
|
+
"""
|
21
|
+
When I run "spec stub_and_mocks_spec.rb --format nested"
|
22
|
+
Then the stdout should match "expected :foo with (\"first\") but received it with ([\"second\"], [\"third\"])"
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Feature: stub implementation
|
2
|
+
|
3
|
+
As an rspec user, I want to stub a complete implementation, not just a
|
4
|
+
return value.
|
5
|
+
|
6
|
+
Scenario: stub implementation
|
7
|
+
Given a file named "stub_implementation.rb" with:
|
8
|
+
"""
|
9
|
+
describe "a stubbed implementation" do
|
10
|
+
it "works" do
|
11
|
+
object = Object.new
|
12
|
+
object.stub(:foo) do |arg|
|
13
|
+
if arg == :this
|
14
|
+
"got this"
|
15
|
+
elsif arg == :that
|
16
|
+
"got that"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
object.foo(:this).should == "got this"
|
21
|
+
object.foo(:that).should == "got that"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
"""
|
25
|
+
When I run "spec stub_implementation.rb"
|
26
|
+
Then the stdout should match "1 example, 0 failures"
|
@@ -0,0 +1,81 @@
|
|
1
|
+
Feature: pending examples
|
2
|
+
|
3
|
+
RSpec offers three ways to indicate that an example is disabled pending
|
4
|
+
some action.
|
5
|
+
|
6
|
+
Scenario: pending implementation
|
7
|
+
Given a file named "example_without_block_spec.rb" with:
|
8
|
+
"""
|
9
|
+
describe "an example" do
|
10
|
+
it "has not yet been implemented"
|
11
|
+
end
|
12
|
+
"""
|
13
|
+
When I run "spec example_without_block_spec.rb"
|
14
|
+
Then the exit code should be 0
|
15
|
+
And the stdout should match "1 example, 0 failures, 1 pending"
|
16
|
+
And the stdout should match "Not Yet Implemented"
|
17
|
+
And the stdout should match "example_without_block_spec.rb:2"
|
18
|
+
|
19
|
+
Scenario: pending implementation with spec/test/unit
|
20
|
+
Given a file named "example_without_block_spec.rb" with:
|
21
|
+
"""
|
22
|
+
require 'spec/test/unit'
|
23
|
+
describe "an example" do
|
24
|
+
it "has not yet been implemented"
|
25
|
+
end
|
26
|
+
"""
|
27
|
+
When I run "spec example_without_block_spec.rb"
|
28
|
+
Then the exit code should be 0
|
29
|
+
And the stdout should match "1 example, 0 failures, 1 pending"
|
30
|
+
And the stdout should match "Not Yet Implemented"
|
31
|
+
And the stdout should match "example_without_block_spec.rb:3"
|
32
|
+
|
33
|
+
Scenario: pending any arbitary reason, with no block
|
34
|
+
Given a file named "pending_without_block_spec.rb" with:
|
35
|
+
"""
|
36
|
+
describe "an example" do
|
37
|
+
it "is implemented but waiting" do
|
38
|
+
pending("something else getting finished")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
"""
|
42
|
+
When I run "spec pending_without_block_spec.rb"
|
43
|
+
Then the exit code should be 0
|
44
|
+
And the stdout should match "1 example, 0 failures, 1 pending"
|
45
|
+
And the stdout should match "(something else getting finished)"
|
46
|
+
And the stdout should match "pending_without_block_spec.rb:2"
|
47
|
+
|
48
|
+
Scenario: pending any arbitary reason, with a block that fails
|
49
|
+
Given a file named "pending_with_failing_block_spec.rb" with:
|
50
|
+
"""
|
51
|
+
describe "an example" do
|
52
|
+
it "is implemented but waiting" do
|
53
|
+
pending("something else getting finished") do
|
54
|
+
raise "this is the failure"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
"""
|
59
|
+
When I run "spec pending_with_failing_block_spec.rb"
|
60
|
+
Then the exit code should be 0
|
61
|
+
And the stdout should match "1 example, 0 failures, 1 pending"
|
62
|
+
And the stdout should match "(something else getting finished)"
|
63
|
+
And the stdout should match "pending_with_failing_block_spec.rb:2"
|
64
|
+
|
65
|
+
Scenario: pending any arbitary reason, with a block that passes
|
66
|
+
Given a file named "pending_with_passing_block_spec.rb" with:
|
67
|
+
"""
|
68
|
+
describe "an example" do
|
69
|
+
it "is implemented but waiting" do
|
70
|
+
pending("something else getting finished") do
|
71
|
+
true.should be(true)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
"""
|
76
|
+
When I run "spec pending_with_passing_block_spec.rb"
|
77
|
+
Then the exit code should be 256
|
78
|
+
And the stdout should match "1 example, 1 failure"
|
79
|
+
And the stdout should match "FIXED"
|
80
|
+
And the stdout should match "Expected pending 'something else getting finished' to fail. No Error was raised."
|
81
|
+
And the stdout should match "pending_with_passing_block_spec.rb:3"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
Feature: run specific examples by line number
|
2
|
+
|
3
|
+
In order to run a single example from command line
|
4
|
+
RSpec allows you to specify the line number of the example(s) to run
|
5
|
+
|
6
|
+
Scenario: --line syntax on single example
|
7
|
+
Given a file named "example_spec.rb" with:
|
8
|
+
"""
|
9
|
+
describe "an example" do
|
10
|
+
it "has not yet been implemented"
|
11
|
+
it "has been implemented" do
|
12
|
+
true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
"""
|
16
|
+
When I run "spec example_spec.rb --line 2"
|
17
|
+
Then the stdout should match "1 example, 0 failures, 1 pending"
|
18
|
+
And the stdout should match "example_spec.rb:2"
|
19
|
+
|
20
|
+
Scenario: colon line syntax on single example
|
21
|
+
Given a file named "example_spec.rb" with:
|
22
|
+
"""
|
23
|
+
describe "an example" do
|
24
|
+
it "has not yet been implemented"
|
25
|
+
it "has been implemented" do
|
26
|
+
true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
"""
|
30
|
+
When I run "spec example_spec.rb:2"
|
31
|
+
Then the stdout should match "1 example, 0 failures, 1 pending"
|
32
|
+
And the stdout should match "example_spec.rb:2"
|
@@ -0,0 +1,169 @@
|
|
1
|
+
Feature: before and after blocks
|
2
|
+
|
3
|
+
As a developer using RSpec
|
4
|
+
I want to execute arbitrary code before and after each example
|
5
|
+
So that I can control the environment in which it is run
|
6
|
+
|
7
|
+
This is supported by the before and after methods which each take a symbol
|
8
|
+
indicating the scope, and a block of code to execute.
|
9
|
+
|
10
|
+
before(:each) blocks are run before each example
|
11
|
+
before(:all) blocks are run once before all of the examples in a group
|
12
|
+
before(:suite) blocks are run once before the entire suite
|
13
|
+
|
14
|
+
after(:each) blocks are run after each example
|
15
|
+
after(:all) blocks are run once after all of the examples in a group
|
16
|
+
after(:suite) blocks are run once after the entire suite
|
17
|
+
|
18
|
+
Before and after blocks are called in the following order:
|
19
|
+
before suite
|
20
|
+
before all
|
21
|
+
before each
|
22
|
+
after each
|
23
|
+
after all
|
24
|
+
after suite
|
25
|
+
|
26
|
+
Before and after blocks can be defined in the example groups to which they
|
27
|
+
apply or in a configuration. When defined in a configuration, they can be
|
28
|
+
applied to all groups or subsets of all groups defined by example group
|
29
|
+
types.
|
30
|
+
|
31
|
+
Scenario: define before(:each) block in example group
|
32
|
+
Given a file named "before_each_in_example_group_spec.rb" with:
|
33
|
+
"""
|
34
|
+
class Thing
|
35
|
+
def widgets
|
36
|
+
@widgets ||= []
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe Thing do
|
41
|
+
before(:each) do
|
42
|
+
@thing = Thing.new
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "initialized in before(:each)" do
|
46
|
+
it "has 0 widgets" do
|
47
|
+
@thing.should have(0).widgets
|
48
|
+
end
|
49
|
+
|
50
|
+
it "can get accept new widgets" do
|
51
|
+
@thing.widgets << Object.new
|
52
|
+
end
|
53
|
+
|
54
|
+
it "does not share state across examples" do
|
55
|
+
@thing.should have(0).widgets
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
"""
|
60
|
+
When I run "rspec before_each_in_example_group_spec.rb"
|
61
|
+
Then the stdout should match "3 examples, 0 failures"
|
62
|
+
|
63
|
+
Scenario: define before(:all) block in example group
|
64
|
+
Given a file named "before_all_in_example_group_spec.rb" with:
|
65
|
+
"""
|
66
|
+
class Thing
|
67
|
+
def widgets
|
68
|
+
@widgets ||= []
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe Thing do
|
73
|
+
before(:all) do
|
74
|
+
@thing = Thing.new
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "initialized in before(:all)" do
|
78
|
+
it "has 0 widgets" do
|
79
|
+
@thing.should have(0).widgets
|
80
|
+
end
|
81
|
+
|
82
|
+
it "can get accept new widgets" do
|
83
|
+
@thing.widgets << Object.new
|
84
|
+
end
|
85
|
+
|
86
|
+
it "shares state across examples" do
|
87
|
+
@thing.should have(1).widgets
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
"""
|
92
|
+
When I run "rspec before_all_in_example_group_spec.rb"
|
93
|
+
Then the stdout should match "3 examples, 0 failures"
|
94
|
+
|
95
|
+
@wip
|
96
|
+
Scenario: define before and after blocks in configuration
|
97
|
+
Given a file named "befores_in_configuration_spec.rb" with:
|
98
|
+
"""
|
99
|
+
Rspec::Core.configure do |config|
|
100
|
+
config.before(:suite) do
|
101
|
+
$before_suite = "before suite"
|
102
|
+
end
|
103
|
+
config.before(:each) do
|
104
|
+
@before_each = "before each"
|
105
|
+
end
|
106
|
+
config.before(:all) do
|
107
|
+
@before_all = "before all"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe "stuff in before blocks" do
|
112
|
+
describe "with :suite" do
|
113
|
+
it "should be available in the example" do
|
114
|
+
$before_suite.should == "before suite"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
describe "with :all" do
|
118
|
+
it "should be available in the example" do
|
119
|
+
@before_all.should == "before all"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
describe "with :each" do
|
123
|
+
it "should be available in the example" do
|
124
|
+
@before_each.should == "before each"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
"""
|
129
|
+
When I run "spec befores_in_configuration_spec.rb"
|
130
|
+
Then the stdout should match "3 examples, 0 failures"
|
131
|
+
|
132
|
+
@wip
|
133
|
+
Scenario: before/after blocks are run in order
|
134
|
+
Given a file named "ensure_block_order_spec.rb" with:
|
135
|
+
"""
|
136
|
+
Rspec::Core.configure do |config|
|
137
|
+
config.before(:suite) do
|
138
|
+
puts "before suite"
|
139
|
+
end
|
140
|
+
config.after(:suite) do
|
141
|
+
puts "after suite"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
describe "before and after callbacks" do
|
146
|
+
before(:all) do
|
147
|
+
puts "before all"
|
148
|
+
end
|
149
|
+
|
150
|
+
before(:each) do
|
151
|
+
puts "before each"
|
152
|
+
end
|
153
|
+
|
154
|
+
after(:each) do
|
155
|
+
puts "after each"
|
156
|
+
end
|
157
|
+
|
158
|
+
after(:all) do
|
159
|
+
puts "after all"
|
160
|
+
end
|
161
|
+
|
162
|
+
it "gets run in order" do
|
163
|
+
|
164
|
+
end
|
165
|
+
end
|
166
|
+
"""
|
167
|
+
When I run "spec ensure_block_order_spec.rb"
|
168
|
+
Then the stdout should match /before suite\nbefore all\nbefore each\nafter each\n\.after all\n.*after suite/m
|
169
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
Feature: customized message
|
2
|
+
|
3
|
+
In order to get the feedback I want
|
4
|
+
As an RSpec user
|
5
|
+
I want to customize the failure message per example
|
6
|
+
|
7
|
+
Scenario: one additional method
|
8
|
+
Given a file named "node_spec.rb.rb" with:
|
9
|
+
"""
|
10
|
+
class Node
|
11
|
+
def initialize(state=:waiting)
|
12
|
+
@state = state
|
13
|
+
end
|
14
|
+
def state
|
15
|
+
@state
|
16
|
+
end
|
17
|
+
def waiting?
|
18
|
+
@state == :waiting
|
19
|
+
end
|
20
|
+
def started?
|
21
|
+
@state == :started
|
22
|
+
end
|
23
|
+
def start
|
24
|
+
@state = :started
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "a new Node" do
|
29
|
+
it "should be waiting" do
|
30
|
+
node = Node.new(:started) #start w/ started to trigger failure
|
31
|
+
node.should be_waiting, "node.state: #{node.state} (first example)"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should not be started" do
|
35
|
+
node = Node.new(:started) #start w/ started to trigger failure
|
36
|
+
node.should_not be_started, "node.state: #{node.state} (second example)"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "node.start" do
|
41
|
+
it "should change the state" do
|
42
|
+
node = Node.new(:started) #start w/ started to trigger failure
|
43
|
+
lambda {node.start}.should change{node.state}, "expected a change"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
"""
|
48
|
+
When I run "spec node_spec.rb.rb --format specdoc"
|
49
|
+
Then the stdout should match "3 examples, 3 failures"
|
50
|
+
And the stdout should not match "to return true, got false"
|
51
|
+
And the stdout should not match "to return false, got true"
|
52
|
+
And the stdout should match "node.state: started (first example)"
|
53
|
+
And the stdout should match "node.state: started (second example)"
|
54
|
+
And the stdout should match "expected a change"
|
@@ -0,0 +1,39 @@
|
|
1
|
+
Feature: define matcher outside rspec
|
2
|
+
|
3
|
+
In order to express my domain clearly in my code examples
|
4
|
+
As a non-rspec user
|
5
|
+
I want a shortcut to define custom matchers
|
6
|
+
|
7
|
+
Scenario: define a matcher with default messages
|
8
|
+
Given a file named "test_multiples.rb" with:
|
9
|
+
"""
|
10
|
+
$:.unshift File.join(File.dirname(__FILE__), "/../../lib")
|
11
|
+
require 'test/unit'
|
12
|
+
require 'rspec/expectations'
|
13
|
+
|
14
|
+
Rspec::Matchers.define :be_a_multiple_of do |expected|
|
15
|
+
match do |actual|
|
16
|
+
actual % expected == 0
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Test::Unit::TestCase
|
21
|
+
include Rspec::Matchers
|
22
|
+
end
|
23
|
+
|
24
|
+
class TestMultiples < Test::Unit::TestCase
|
25
|
+
|
26
|
+
def test_9_should_be_a_multiple_of_3
|
27
|
+
9.should be_a_multiple_of(3)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_9_should_be_a_multiple_of_4
|
31
|
+
9.should be_a_multiple_of(4)
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
"""
|
36
|
+
When I run "ruby test_multiples.rb"
|
37
|
+
Then the exit code should be 256
|
38
|
+
And the stdout should match "expected 9 to be a multiple of 4"
|
39
|
+
And the stdout should match "2 tests, 0 assertions, 1 failures, 0 errors"
|