rspec-core 2.0.0.beta.22 → 2.6.4
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/.gitignore +1 -0
- data/.rspec +0 -1
- data/.travis.yml +9 -0
- data/Changelog.md +305 -0
- data/Gemfile +45 -20
- data/Guardfile +5 -0
- data/License.txt +2 -1
- data/{README.markdown → README.md} +23 -5
- data/Rakefile +63 -32
- data/bin/autospec +13 -0
- data/bin/rspec +24 -2
- data/cucumber.yml +1 -1
- data/features/.nav +57 -0
- data/features/Autotest.md +38 -0
- data/features/README.md +17 -0
- data/features/Upgrade.md +320 -0
- data/features/command_line/README.md +28 -0
- data/features/command_line/configure.feature +18 -15
- data/features/command_line/example_name_option.feature +37 -23
- data/features/command_line/exit_status.feature +16 -31
- data/features/command_line/format_option.feature +73 -0
- data/features/command_line/line_number_appended_to_path.feature +25 -27
- data/features/command_line/line_number_option.feature +11 -10
- data/features/command_line/rake_task.feature +68 -0
- data/features/command_line/tag.feature +90 -0
- data/features/configuration/alias_example_to.feature +48 -0
- data/features/configuration/custom_settings.feature +8 -10
- data/features/configuration/fail_fast.feature +77 -0
- data/features/configuration/read_options_from_file.feature +42 -26
- data/features/example_groups/basic_structure.feature +55 -0
- data/features/example_groups/shared_context.feature +74 -0
- data/features/example_groups/shared_example_group.feature +56 -41
- data/features/expectation_framework_integration/configure_expectation_framework.feature +73 -0
- data/features/filtering/exclusion_filters.feature +69 -9
- data/features/filtering/if_and_unless.feature +168 -0
- data/features/filtering/inclusion_filters.feature +58 -26
- data/features/filtering/run_all_when_everything_filtered.feature +46 -0
- data/features/formatters/custom_formatter.feature +17 -13
- data/features/formatters/text_formatter.feature +43 -0
- data/features/helper_methods/arbitrary_methods.feature +40 -0
- data/features/helper_methods/let.feature +50 -0
- data/features/helper_methods/modules.feature +149 -0
- data/features/hooks/around_hooks.feature +99 -69
- data/features/hooks/before_and_after_hooks.feature +74 -40
- data/features/hooks/filtering.feature +227 -0
- data/features/metadata/current_example.feature +17 -0
- data/features/metadata/described_class.feature +17 -0
- data/features/metadata/user_defined.feature +111 -0
- data/features/mock_framework_integration/use_any_framework.feature +106 -0
- data/features/mock_framework_integration/use_flexmock.feature +84 -11
- data/features/mock_framework_integration/use_mocha.feature +85 -11
- data/features/mock_framework_integration/use_rr.feature +86 -11
- data/features/mock_framework_integration/use_rspec.feature +85 -11
- data/features/pending/pending_examples.feature +143 -5
- data/features/spec_files/arbitrary_file_suffix.feature +2 -2
- data/features/step_definitions/additional_cli_steps.rb +30 -0
- data/features/subject/attribute_of_subject.feature +93 -15
- data/features/subject/explicit_subject.feature +28 -17
- data/features/subject/implicit_receiver.feature +29 -0
- data/features/subject/implicit_subject.feature +9 -10
- data/features/support/env.rb +6 -1
- data/lib/autotest/discover.rb +1 -0
- data/lib/autotest/rspec2.rb +15 -11
- data/lib/rspec/autorun.rb +2 -0
- data/lib/rspec/core/backward_compatibility.rb +32 -3
- data/lib/rspec/core/command_line.rb +4 -28
- data/lib/rspec/core/command_line_configuration.rb +16 -16
- data/lib/rspec/core/configuration.rb +286 -89
- data/lib/rspec/core/configuration_options.rb +74 -40
- data/lib/rspec/core/deprecation.rb +1 -1
- data/lib/rspec/core/drb_command_line.rb +5 -11
- data/lib/rspec/core/dsl.rb +11 -0
- data/lib/rspec/core/example.rb +63 -39
- data/lib/rspec/core/example_group.rb +109 -59
- data/lib/rspec/core/expecting/with_rspec.rb +9 -0
- data/lib/rspec/core/expecting/with_stdlib.rb +9 -0
- data/lib/rspec/core/extensions/kernel.rb +1 -1
- data/lib/rspec/core/extensions.rb +0 -1
- data/lib/rspec/core/formatters/base_formatter.rb +30 -15
- data/lib/rspec/core/formatters/base_text_formatter.rb +68 -40
- data/lib/rspec/core/formatters/documentation_formatter.rb +4 -2
- data/lib/rspec/core/formatters/helpers.rb +0 -4
- data/lib/rspec/core/formatters/html_formatter.rb +146 -41
- data/lib/rspec/core/formatters/progress_formatter.rb +1 -0
- data/lib/rspec/core/formatters/snippet_extractor.rb +1 -1
- data/lib/rspec/core/formatters/text_mate_formatter.rb +3 -1
- data/lib/rspec/core/hooks.rb +56 -17
- data/lib/rspec/core/metadata.rb +75 -64
- data/lib/rspec/core/metadata_hash_builder.rb +93 -0
- data/lib/rspec/core/mocking/with_flexmock.rb +2 -0
- data/lib/rspec/core/mocking/with_mocha.rb +2 -0
- data/lib/rspec/core/mocking/with_rr.rb +2 -0
- data/lib/rspec/core/mocking/with_rspec.rb +3 -1
- data/lib/rspec/core/option_parser.rb +49 -7
- data/lib/rspec/core/pending.rb +22 -4
- data/lib/rspec/core/rake_task.rb +64 -28
- data/lib/rspec/core/reporter.rb +6 -5
- data/lib/rspec/core/ruby_project.rb +2 -2
- data/lib/rspec/core/runner.rb +50 -6
- data/lib/rspec/core/shared_context.rb +16 -0
- data/lib/rspec/core/shared_example_group.rb +19 -4
- data/lib/rspec/core/subject.rb +92 -65
- data/lib/rspec/core/version.rb +1 -1
- data/lib/rspec/core/world.rb +85 -27
- data/lib/rspec/core.rb +55 -24
- data/lib/rspec/monkey/spork/test_framework/rspec.rb +1 -0
- data/rspec-core.gemspec +4 -25
- data/script/FullBuildRakeFile +63 -0
- data/script/cucumber +1 -0
- data/script/full_build +1 -0
- data/script/spec +1 -0
- data/spec/autotest/discover_spec.rb +19 -0
- data/spec/autotest/failed_results_re_spec.rb +25 -9
- data/spec/autotest/rspec_spec.rb +32 -41
- data/spec/rspec/core/command_line_spec.rb +62 -7
- data/spec/rspec/core/configuration_options_spec.rb +216 -148
- data/spec/rspec/core/configuration_spec.rb +419 -108
- data/spec/rspec/core/deprecations_spec.rb +38 -1
- data/spec/rspec/core/drb_command_line_spec.rb +21 -56
- data/spec/rspec/core/example_group_spec.rb +366 -127
- data/spec/rspec/core/example_spec.rb +125 -45
- data/spec/rspec/core/formatters/base_formatter_spec.rb +61 -1
- data/spec/rspec/core/formatters/base_text_formatter_spec.rb +134 -97
- data/spec/rspec/core/formatters/documentation_formatter_spec.rb +7 -6
- data/spec/rspec/core/formatters/helpers_spec.rb +1 -1
- data/spec/rspec/core/formatters/html_formatted-1.8.6.html +199 -81
- data/spec/rspec/core/formatters/html_formatted-1.8.7-jruby.html +195 -83
- data/spec/rspec/core/formatters/html_formatted-1.8.7.html +199 -81
- data/spec/rspec/core/formatters/html_formatted-1.9.1.html +206 -81
- data/spec/rspec/core/formatters/html_formatted-1.9.2.html +206 -61
- data/spec/rspec/core/formatters/html_formatter_spec.rb +17 -9
- data/spec/rspec/core/formatters/progress_formatter_spec.rb +1 -1
- data/spec/rspec/core/formatters/text_mate_formatted-1.8.6.html +199 -81
- data/spec/rspec/core/formatters/text_mate_formatted-1.8.7-jruby.html +195 -81
- data/spec/rspec/core/formatters/text_mate_formatted-1.8.7.html +199 -81
- data/spec/rspec/core/formatters/text_mate_formatted-1.9.1.html +206 -81
- data/spec/rspec/core/formatters/text_mate_formatted-1.9.2.html +206 -81
- data/spec/rspec/core/formatters/text_mate_formatter_spec.rb +22 -7
- data/spec/rspec/core/hooks_filtering_spec.rb +128 -5
- data/spec/rspec/core/hooks_spec.rb +90 -4
- data/spec/rspec/core/metadata_spec.rb +176 -163
- data/spec/rspec/core/option_parser_spec.rb +73 -6
- data/spec/rspec/core/pending_example_spec.rb +137 -35
- data/spec/rspec/core/rake_task_spec.rb +62 -29
- data/spec/rspec/core/reporter_spec.rb +20 -4
- data/spec/rspec/core/resources/formatter_specs.rb +25 -1
- data/spec/rspec/core/rspec_matchers_spec.rb +45 -0
- data/spec/rspec/core/runner_spec.rb +60 -10
- data/spec/rspec/core/shared_context_spec.rb +30 -0
- data/spec/rspec/core/shared_example_group_spec.rb +59 -23
- data/spec/rspec/core/subject_spec.rb +136 -0
- data/spec/rspec/core/world_spec.rb +211 -68
- data/spec/rspec/core_spec.rb +28 -0
- data/spec/spec_helper.rb +41 -24
- data/spec/support/matchers.rb +44 -13
- data/spec/support/shared_example_groups.rb +41 -0
- data/spec/support/spec_files.rb +44 -0
- data/spec.txt +1126 -0
- metadata +100 -170
- data/.treasure_map.rb +0 -23
- data/History.md +0 -30
- data/Upgrade.markdown +0 -150
- data/autotest/discover.rb +0 -2
- data/features/README.markdown +0 -12
- data/features/example_groups/describe_aliases.feature +0 -25
- data/features/example_groups/nested_groups.feature +0 -44
- data/features/hooks/described_class.feature +0 -14
- data/features/hooks/halt.feature +0 -26
- data/lib/rspec/core/around_proxy.rb +0 -14
- data/lib/rspec/core/extensions/object.rb +0 -15
- data/lib/rspec/core/formatters.rb +0 -8
- data/spec/ruby_forker.rb +0 -13
- data/specs.watchr +0 -59
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
Feature: inclusion filters
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
|
|
3
|
+
You can restrict which examples are run by declaring an inclusion filter. The
|
|
4
|
+
most common use case is to focus on a subset of examples as you're focused on
|
|
5
|
+
a particular problem.
|
|
6
|
+
|
|
7
|
+
If you set the `treat_symbols_as_metadata_keys_with_true_values` config option
|
|
8
|
+
to `true`, you can specify metadata using only symbols.
|
|
9
|
+
|
|
10
|
+
Background:
|
|
11
|
+
Given a file named "spec/spec_helper.rb" with:
|
|
5
12
|
"""
|
|
6
13
|
RSpec.configure do |c|
|
|
14
|
+
# filter_run is short-form alias for filter_run_including
|
|
7
15
|
c.filter_run :focus => true
|
|
8
16
|
end
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
Scenario: focus on an example
|
|
20
|
+
Given a file named "spec/sample_spec.rb" with:
|
|
21
|
+
"""
|
|
22
|
+
require "spec_helper"
|
|
9
23
|
|
|
10
24
|
describe "something" do
|
|
11
25
|
it "does one thing" do
|
|
@@ -15,16 +29,14 @@ Feature: inclusion filters
|
|
|
15
29
|
end
|
|
16
30
|
end
|
|
17
31
|
"""
|
|
18
|
-
When I run
|
|
32
|
+
When I run `rspec spec/sample_spec.rb --format doc`
|
|
19
33
|
Then the output should contain "does another thing"
|
|
20
34
|
And the output should not contain "does one thing"
|
|
21
35
|
|
|
22
|
-
Scenario: focus on
|
|
36
|
+
Scenario: focus on a group
|
|
23
37
|
Given a file named "spec/sample_spec.rb" with:
|
|
24
38
|
"""
|
|
25
|
-
|
|
26
|
-
c.filter_run :focus => true
|
|
27
|
-
end
|
|
39
|
+
require "spec_helper"
|
|
28
40
|
|
|
29
41
|
describe "group 1", :focus => true do
|
|
30
42
|
it "group 1 example 1" do
|
|
@@ -39,36 +51,56 @@ Feature: inclusion filters
|
|
|
39
51
|
end
|
|
40
52
|
end
|
|
41
53
|
"""
|
|
42
|
-
When I run
|
|
54
|
+
When I run `rspec spec/sample_spec.rb --format doc`
|
|
43
55
|
Then the output should contain "group 1 example 1"
|
|
44
56
|
And the output should contain "group 1 example 2"
|
|
45
57
|
And the output should not contain "group 2 example 1"
|
|
46
58
|
|
|
47
|
-
Scenario:
|
|
48
|
-
Given a file named "spec/
|
|
59
|
+
Scenario: before/after(:all) hooks in unmatched example group are not run
|
|
60
|
+
Given a file named "spec/before_after_all_inclusion_filter_spec.rb" with:
|
|
49
61
|
"""
|
|
50
|
-
|
|
51
|
-
c.filter_run :focus => true
|
|
52
|
-
c.run_all_when_everything_filtered = true
|
|
53
|
-
end
|
|
62
|
+
require "spec_helper"
|
|
54
63
|
|
|
55
|
-
describe "group 1" do
|
|
56
|
-
|
|
57
|
-
|
|
64
|
+
describe "group 1", :focus => true do
|
|
65
|
+
before(:all) { puts "before all in focused group" }
|
|
66
|
+
after(:all) { puts "after all in focused group" }
|
|
58
67
|
|
|
59
|
-
it "group 1 example
|
|
68
|
+
it "group 1 example" do
|
|
60
69
|
end
|
|
61
70
|
end
|
|
62
71
|
|
|
63
72
|
describe "group 2" do
|
|
64
|
-
|
|
73
|
+
before(:all) { puts "before all in unfocused group" }
|
|
74
|
+
after(:all) { puts "after all in unfocused group" }
|
|
75
|
+
|
|
76
|
+
context "context 1" do
|
|
77
|
+
it "group 2 context 1 example 1" do
|
|
78
|
+
end
|
|
65
79
|
end
|
|
66
80
|
end
|
|
67
81
|
"""
|
|
68
|
-
When I run
|
|
69
|
-
Then the output should contain "
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
And the output should contain "3 examples, 0 failures"
|
|
82
|
+
When I run `rspec ./spec/before_after_all_inclusion_filter_spec.rb`
|
|
83
|
+
Then the output should contain "before all in focused group"
|
|
84
|
+
And the output should contain "after all in focused group"
|
|
85
|
+
And the output should not contain "before all in unfocused group"
|
|
86
|
+
And the output should not contain "after all in unfocused group"
|
|
74
87
|
|
|
88
|
+
Scenario: Use symbols as metadata
|
|
89
|
+
Given a file named "symbols_as_metadata_spec.rb" with:
|
|
90
|
+
"""
|
|
91
|
+
RSpec.configure do |c|
|
|
92
|
+
c.treat_symbols_as_metadata_keys_with_true_values = true
|
|
93
|
+
c.filter_run :current_example
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
describe "something" do
|
|
97
|
+
it "does one thing" do
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it "does another thing", :current_example do
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
"""
|
|
104
|
+
When I run `rspec symbols_as_metadata_spec.rb --format doc`
|
|
105
|
+
Then the output should contain "does another thing"
|
|
106
|
+
And the output should not contain "does one thing"
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
Feature: run all when everything filtered
|
|
2
|
+
|
|
3
|
+
Use the run_all_when_everything_filtered configuration option to do just
|
|
4
|
+
that. This works well when paired with an inclusion filter like ":focus =>
|
|
5
|
+
true", as it will run all the examples when none match the inclusion filter.
|
|
6
|
+
|
|
7
|
+
Background:
|
|
8
|
+
Given a file named "spec/spec_helper.rb" with:
|
|
9
|
+
"""
|
|
10
|
+
RSpec.configure do |c|
|
|
11
|
+
c.filter_run :focus => true
|
|
12
|
+
c.run_all_when_everything_filtered = true
|
|
13
|
+
end
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
Scenario: no examples match filter (runs all examples)
|
|
17
|
+
Given a file named "spec/sample_spec.rb" with:
|
|
18
|
+
"""
|
|
19
|
+
require "spec_helper"
|
|
20
|
+
|
|
21
|
+
describe "group 1" do
|
|
22
|
+
it "group 1 example 1" do
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "group 1 example 2" do
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
describe "group 2" do
|
|
30
|
+
it "group 2 example 1" do
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
"""
|
|
34
|
+
When I run `rspec spec/sample_spec.rb --format doc`
|
|
35
|
+
Then the output should contain "No examples matched {:focus=>true}"
|
|
36
|
+
And the examples should all pass
|
|
37
|
+
And the output should contain:
|
|
38
|
+
"""
|
|
39
|
+
group 1
|
|
40
|
+
group 1 example 1
|
|
41
|
+
group 1 example 2
|
|
42
|
+
|
|
43
|
+
group 2
|
|
44
|
+
group 2 example 1
|
|
45
|
+
"""
|
|
46
|
+
|
|
@@ -1,32 +1,36 @@
|
|
|
1
1
|
Feature: custom formatters
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
RSpec ships with general purpose output formatters. You can tell RSpec which
|
|
4
|
+
one to use using the [`--format` command line
|
|
5
|
+
option]('../command_line/format_option').
|
|
6
|
+
|
|
7
|
+
When RSpec's built-in output formatters don't, however, give you everything
|
|
8
|
+
you need, you can write your own custom formatter and tell RSpec to use that
|
|
9
|
+
one instead. The simplest way is to subclass RSpec's `BaseTextFormatter`,
|
|
10
|
+
and then override just the methods that you want to modify.
|
|
6
11
|
|
|
7
|
-
|
|
8
|
-
Scenario: specdoc format
|
|
12
|
+
Scenario: custom formatter
|
|
9
13
|
Given a file named "custom_formatter.rb" with:
|
|
10
14
|
"""
|
|
11
|
-
require "rspec/
|
|
15
|
+
require "rspec/core/formatters/base_text_formatter"
|
|
12
16
|
|
|
13
|
-
class CustomFormatter < RSpec::
|
|
14
|
-
def initialize(
|
|
15
|
-
|
|
17
|
+
class CustomFormatter < RSpec::Core::Formatters::BaseTextFormatter
|
|
18
|
+
def initialize(output)
|
|
19
|
+
super(output)
|
|
16
20
|
end
|
|
21
|
+
|
|
17
22
|
def example_started(proxy)
|
|
18
|
-
|
|
23
|
+
output << "example: " << proxy.description
|
|
19
24
|
end
|
|
20
25
|
end
|
|
21
26
|
"""
|
|
22
|
-
And a file named "
|
|
27
|
+
And a file named "example_spec.rb" with:
|
|
23
28
|
"""
|
|
24
29
|
describe "my group" do
|
|
25
30
|
specify "my example" do
|
|
26
31
|
end
|
|
27
32
|
end
|
|
28
33
|
"""
|
|
29
|
-
|
|
30
|
-
When I run "rspec ./simple_example_spec.rb --require custom_formatter.rb --format CustomFormatter"
|
|
34
|
+
When I run `rspec example_spec.rb --require ./custom_formatter.rb --format CustomFormatter`
|
|
31
35
|
Then the output should contain "example: my example"
|
|
32
36
|
And the exit status should be 0
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
Feature: text formatter
|
|
2
|
+
|
|
3
|
+
In order to easily see the result of running my specs
|
|
4
|
+
As an RSpec user
|
|
5
|
+
I want clear, concise, well-formatted output
|
|
6
|
+
|
|
7
|
+
Scenario: Backtrace formatting for failing specs in multiple files
|
|
8
|
+
Given a file named "string_spec.rb" with:
|
|
9
|
+
"""
|
|
10
|
+
describe String do
|
|
11
|
+
it "has a failing example" do
|
|
12
|
+
"foo".reverse.should == "ofo"
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
"""
|
|
16
|
+
And a file named "integer_spec.rb" with:
|
|
17
|
+
"""
|
|
18
|
+
require 'rspec/autorun'
|
|
19
|
+
|
|
20
|
+
describe Integer do
|
|
21
|
+
it "has a failing example" do
|
|
22
|
+
(7 + 5).should == 11
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
"""
|
|
26
|
+
When I run `ruby ./integer_spec.rb ./string_spec.rb`
|
|
27
|
+
Then the backtrace-normalized output should contain:
|
|
28
|
+
"""
|
|
29
|
+
Failures:
|
|
30
|
+
|
|
31
|
+
1) Integer has a failing example
|
|
32
|
+
Failure/Error: (7 + 5).should == 11
|
|
33
|
+
expected: 11
|
|
34
|
+
got: 12 (using ==)
|
|
35
|
+
# ./integer_spec.rb:5
|
|
36
|
+
|
|
37
|
+
2) String has a failing example
|
|
38
|
+
Failure/Error: "foo".reverse.should == "ofo"
|
|
39
|
+
expected: "ofo"
|
|
40
|
+
got: "oof" (using ==)
|
|
41
|
+
# ./string_spec.rb:3
|
|
42
|
+
"""
|
|
43
|
+
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
Feature: arbitrary helper methods
|
|
2
|
+
|
|
3
|
+
You can define methods in any example group using Ruby's `def` keyword or
|
|
4
|
+
`define_method` method. These _helper_ methods are exposed to examples in the
|
|
5
|
+
group in which they are defined and groups nested within that group, but not
|
|
6
|
+
parent or sibling groups.
|
|
7
|
+
|
|
8
|
+
Scenario: use a method defined in the same group
|
|
9
|
+
Given a file named "example_spec.rb" with:
|
|
10
|
+
"""
|
|
11
|
+
describe "an example" do
|
|
12
|
+
def help
|
|
13
|
+
:available
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "has access to methods defined in its group" do
|
|
17
|
+
help.should be(:available)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
"""
|
|
21
|
+
When I run `rspec example_spec.rb`
|
|
22
|
+
Then the examples should all pass
|
|
23
|
+
|
|
24
|
+
Scenario: use a method defined in a parent group
|
|
25
|
+
Given a file named "example_spec.rb" with:
|
|
26
|
+
"""
|
|
27
|
+
describe "an example" do
|
|
28
|
+
def help
|
|
29
|
+
:available
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
describe "in a nested group" do
|
|
33
|
+
it "has access to methods defined in its parent group" do
|
|
34
|
+
help.should be(:available)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
"""
|
|
39
|
+
When I run `rspec example_spec.rb`
|
|
40
|
+
Then the examples should all pass
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Feature: let and let!
|
|
2
|
+
|
|
3
|
+
Use `let` to define a memoized helper method. The value will be cached
|
|
4
|
+
across multiple calls in the same example but not across examples.
|
|
5
|
+
|
|
6
|
+
Note that `let` is lazy-evaluated: it is not evaluated until the first time
|
|
7
|
+
the method it defines is invoked. You can use `let!` to force the method's
|
|
8
|
+
invocation before each example.
|
|
9
|
+
|
|
10
|
+
Scenario: use let to define memoized helper method
|
|
11
|
+
Given a file named "let_spec.rb" with:
|
|
12
|
+
"""
|
|
13
|
+
$count = 0
|
|
14
|
+
describe "let" do
|
|
15
|
+
let(:count) { $count += 1 }
|
|
16
|
+
|
|
17
|
+
it "memoizes the value" do
|
|
18
|
+
count.should == 1
|
|
19
|
+
count.should == 1
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "is not cached across examples" do
|
|
23
|
+
count.should == 2
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
"""
|
|
27
|
+
When I run `rspec let_spec.rb`
|
|
28
|
+
Then the examples should all pass
|
|
29
|
+
|
|
30
|
+
Scenario: use let! to define a memoized helper method that is called in a before hook
|
|
31
|
+
Given a file named "let_bang_spec.rb" with:
|
|
32
|
+
"""
|
|
33
|
+
$count = 0
|
|
34
|
+
describe "let!" do
|
|
35
|
+
invocation_order = []
|
|
36
|
+
|
|
37
|
+
let!(:count) do
|
|
38
|
+
invocation_order << :let!
|
|
39
|
+
$count += 1
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "calls the helper method in a before hook" do
|
|
43
|
+
invocation_order << :example
|
|
44
|
+
invocation_order.should == [:let!, :example]
|
|
45
|
+
count.should == 1
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
"""
|
|
49
|
+
When I run `rspec let_bang_spec.rb`
|
|
50
|
+
Then the examples should all pass
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
Feature: Define helper methods in a module
|
|
2
|
+
|
|
3
|
+
You can define helper methods in a module and include it in
|
|
4
|
+
your example groups using the `config.include` configuration
|
|
5
|
+
option. `config.extend` can be used to extend the module onto
|
|
6
|
+
your example groups so that the methods in the module are available
|
|
7
|
+
in the example groups themselves (but not in the actual examples).
|
|
8
|
+
|
|
9
|
+
You can also include or extend the module onto only certain example
|
|
10
|
+
groups by passing a metadata hash as the last argument. Only groups
|
|
11
|
+
that match the given metadata will include or extend the module.
|
|
12
|
+
|
|
13
|
+
If you set the `treat_symbols_as_metadata_keys_with_true_values` config option
|
|
14
|
+
to `true`, you can specify metadata using only symbols.
|
|
15
|
+
|
|
16
|
+
Background:
|
|
17
|
+
Given a file named "helpers.rb" with:
|
|
18
|
+
"""
|
|
19
|
+
module Helpers
|
|
20
|
+
def help
|
|
21
|
+
:available
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
Scenario: include a module in all example groups
|
|
27
|
+
Given a file named "include_module_spec.rb" with:
|
|
28
|
+
"""
|
|
29
|
+
require './helpers'
|
|
30
|
+
|
|
31
|
+
RSpec.configure do |c|
|
|
32
|
+
c.include Helpers
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
describe "an example group" do
|
|
36
|
+
it "has access the helper methods defined in the module" do
|
|
37
|
+
help.should be(:available)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
"""
|
|
41
|
+
When I run `rspec include_module_spec.rb`
|
|
42
|
+
Then the examples should all pass
|
|
43
|
+
|
|
44
|
+
Scenario: extend a module in all example groups
|
|
45
|
+
Given a file named "extend_module_spec.rb" with:
|
|
46
|
+
"""
|
|
47
|
+
require './helpers'
|
|
48
|
+
|
|
49
|
+
RSpec.configure do |c|
|
|
50
|
+
c.extend Helpers
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
describe "an example group" do
|
|
54
|
+
puts "Help is #{help}"
|
|
55
|
+
|
|
56
|
+
it "does not have access to the helper methods defined in the module" do
|
|
57
|
+
expect { help }.to raise_error(NameError)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
"""
|
|
61
|
+
When I run `rspec extend_module_spec.rb`
|
|
62
|
+
Then the examples should all pass
|
|
63
|
+
And the output should contain "Help is available"
|
|
64
|
+
|
|
65
|
+
Scenario: include a module in only some example groups
|
|
66
|
+
Given a file named "include_module_in_some_groups_spec.rb" with:
|
|
67
|
+
"""
|
|
68
|
+
require './helpers'
|
|
69
|
+
|
|
70
|
+
RSpec.configure do |c|
|
|
71
|
+
c.include Helpers, :foo => :bar
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
describe "an example group with matching metadata", :foo => :bar do
|
|
75
|
+
it "has access the helper methods defined in the module" do
|
|
76
|
+
help.should be(:available)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
describe "an example group without matching metadata" do
|
|
81
|
+
it "does not have access to the helper methods defined in the module" do
|
|
82
|
+
expect { help }.to raise_error(NameError)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
"""
|
|
86
|
+
When I run `rspec include_module_in_some_groups_spec.rb`
|
|
87
|
+
Then the examples should all pass
|
|
88
|
+
|
|
89
|
+
Scenario: extend a module in only some example groups
|
|
90
|
+
Given a file named "extend_module_in_only_some_groups_spec.rb" with:
|
|
91
|
+
"""
|
|
92
|
+
require './helpers'
|
|
93
|
+
|
|
94
|
+
RSpec.configure do |c|
|
|
95
|
+
c.extend Helpers, :foo => :bar
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
describe "an example group with matching metadata", :foo => :bar do
|
|
99
|
+
puts "In a matching group, help is #{help}"
|
|
100
|
+
|
|
101
|
+
it "does not have access to the helper methods defined in the module" do
|
|
102
|
+
expect { help }.to raise_error(NameError)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
describe "an example group without matching metadata" do
|
|
107
|
+
puts "In a non-matching group, help is #{help rescue 'not available'}"
|
|
108
|
+
|
|
109
|
+
it "does not have access to the helper methods defined in the module" do
|
|
110
|
+
expect { help }.to raise_error(NameError)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
"""
|
|
114
|
+
When I run `rspec extend_module_in_only_some_groups_spec.rb`
|
|
115
|
+
Then the examples should all pass
|
|
116
|
+
And the output should contain "In a matching group, help is available"
|
|
117
|
+
And the output should contain "In a non-matching group, help is not available"
|
|
118
|
+
|
|
119
|
+
Scenario: use symbols as metadata
|
|
120
|
+
Given a file named "symbols_as_metadata_spec.rb" with:
|
|
121
|
+
"""
|
|
122
|
+
require './helpers'
|
|
123
|
+
|
|
124
|
+
RSpec.configure do |c|
|
|
125
|
+
c.treat_symbols_as_metadata_keys_with_true_values = true
|
|
126
|
+
c.include Helpers, :include_helpers
|
|
127
|
+
c.extend Helpers, :extend_helpers
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
describe "an example group with matching include metadata", :include_helpers do
|
|
131
|
+
puts "In a group not matching the extend filter, help is #{help rescue 'not available'}"
|
|
132
|
+
|
|
133
|
+
it "has access the helper methods defined in the module" do
|
|
134
|
+
help.should be(:available)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
describe "an example group with matching extend metadata", :extend_helpers do
|
|
139
|
+
puts "In a group matching the extend filter, help is #{help}"
|
|
140
|
+
|
|
141
|
+
it "does not have access to the helper methods defined in the module" do
|
|
142
|
+
expect { help }.to raise_error(NameError)
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
"""
|
|
146
|
+
When I run `rspec symbols_as_metadata_spec.rb`
|
|
147
|
+
Then the examples should all pass
|
|
148
|
+
And the output should contain "In a group not matching the extend filter, help is not available"
|
|
149
|
+
And the output should contain "In a group matching the extend filter, help is available"
|