rspec-core 3.0.0.beta2 → 3.0.0.rc1
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.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/.yardopts +1 -0
- data/Changelog.md +297 -57
- data/README.md +16 -13
- data/lib/rspec/core.rb +55 -84
- data/lib/rspec/core/backport_random.rb +35 -3
- data/lib/rspec/core/backtrace_formatter.rb +4 -13
- data/lib/rspec/core/configuration.rb +330 -114
- data/lib/rspec/core/configuration_options.rb +38 -22
- data/lib/rspec/core/drb.rb +111 -0
- data/lib/rspec/core/dsl.rb +8 -2
- data/lib/rspec/core/example.rb +203 -94
- data/lib/rspec/core/example_group.rb +344 -316
- data/lib/rspec/core/filter_manager.rb +135 -90
- data/lib/rspec/core/flat_map.rb +1 -0
- data/lib/rspec/core/formatters.rb +50 -14
- data/lib/rspec/core/formatters/base_formatter.rb +32 -138
- data/lib/rspec/core/formatters/base_text_formatter.rb +32 -253
- data/lib/rspec/core/formatters/console_codes.rb +65 -0
- data/lib/rspec/core/formatters/deprecation_formatter.rb +24 -15
- data/lib/rspec/core/formatters/documentation_formatter.rb +7 -10
- data/lib/rspec/core/formatters/helpers.rb +15 -9
- data/lib/rspec/core/formatters/html_formatter.rb +17 -16
- data/lib/rspec/core/formatters/html_printer.rb +1 -0
- data/lib/rspec/core/formatters/json_formatter.rb +18 -20
- data/lib/rspec/core/formatters/profile_formatter.rb +67 -0
- data/lib/rspec/core/formatters/progress_formatter.rb +6 -7
- data/lib/rspec/core/formatters/snippet_extractor.rb +8 -6
- data/lib/rspec/core/hooks.rb +131 -125
- data/lib/rspec/core/memoized_helpers.rb +31 -26
- data/lib/rspec/core/metadata.rb +277 -184
- data/lib/rspec/core/metadata_filter.rb +86 -0
- data/lib/rspec/core/minitest_assertions_adapter.rb +28 -0
- data/lib/rspec/core/mocking_adapters/flexmock.rb +1 -1
- data/lib/rspec/core/mocking_adapters/mocha.rb +1 -1
- data/lib/rspec/core/mocking_adapters/null.rb +1 -1
- data/lib/rspec/core/mocking_adapters/rr.rb +2 -1
- data/lib/rspec/core/mocking_adapters/rspec.rb +1 -1
- data/lib/rspec/core/notifications.rb +435 -24
- data/lib/rspec/core/option_parser.rb +16 -25
- data/lib/rspec/core/ordering.rb +3 -1
- data/lib/rspec/core/pending.rb +57 -33
- data/lib/rspec/core/project_initializer.rb +2 -0
- data/lib/rspec/core/project_initializer/spec_helper.rb +5 -4
- data/lib/rspec/core/rake_task.rb +45 -20
- data/lib/rspec/core/reporter.rb +50 -22
- data/lib/rspec/core/ruby_project.rb +1 -0
- data/lib/rspec/core/runner.rb +93 -39
- data/lib/rspec/core/shared_context.rb +7 -5
- data/lib/rspec/core/shared_example_group.rb +85 -77
- data/lib/rspec/core/test_unit_assertions_adapter.rb +30 -0
- data/lib/rspec/core/version.rb +3 -1
- data/lib/rspec/core/warnings.rb +35 -17
- data/lib/rspec/core/world.rb +57 -5
- metadata +56 -369
- metadata.gz.sig +3 -3
- data/features/README.md +0 -13
- data/features/Upgrade.md +0 -352
- data/features/command_line/README.md +0 -25
- data/features/command_line/dry_run.feature +0 -29
- data/features/command_line/example_name_option.feature +0 -97
- data/features/command_line/exit_status.feature +0 -82
- data/features/command_line/fail_fast.feature +0 -26
- data/features/command_line/format_option.feature +0 -75
- data/features/command_line/init.feature +0 -57
- data/features/command_line/line_number_appended_to_path.feature +0 -140
- data/features/command_line/line_number_option.feature +0 -58
- data/features/command_line/order.feature +0 -25
- data/features/command_line/pattern_option.feature +0 -49
- data/features/command_line/rake_task.feature +0 -122
- data/features/command_line/randomization.feature +0 -63
- data/features/command_line/require_option.feature +0 -43
- data/features/command_line/ruby.feature +0 -23
- data/features/command_line/tag.feature +0 -98
- data/features/command_line/warnings_option.feature +0 -29
- data/features/configuration/alias_example_to.feature +0 -39
- data/features/configuration/backtrace_exclusion_patterns.feature +0 -105
- data/features/configuration/custom_settings.feature +0 -84
- data/features/configuration/default_path.feature +0 -38
- data/features/configuration/deprecation_stream.feature +0 -58
- data/features/configuration/enable_global_dsl.feature +0 -54
- data/features/configuration/fail_fast.feature +0 -77
- data/features/configuration/failure_exit_code.feature +0 -36
- data/features/configuration/order_and_seed.feature +0 -3
- data/features/configuration/output_stream.feature +0 -24
- data/features/configuration/overriding_global_ordering.feature +0 -93
- data/features/configuration/pattern.feature +0 -38
- data/features/configuration/profile.feature +0 -220
- data/features/configuration/read_options_from_file.feature +0 -90
- data/features/configuration/run_all_when_everything_filtered.feature +0 -76
- data/features/example_groups/aliasing.feature +0 -48
- data/features/example_groups/basic_structure.feature +0 -55
- data/features/example_groups/shared_context.feature +0 -74
- data/features/example_groups/shared_examples.feature +0 -286
- data/features/expectation_framework_integration/configure_expectation_framework.feature +0 -102
- data/features/filtering/exclusion_filters.feature +0 -135
- data/features/filtering/if_and_unless.feature +0 -138
- data/features/filtering/inclusion_filters.feature +0 -101
- data/features/formatters/configurable_colors.feature +0 -31
- data/features/formatters/custom_formatter.feature +0 -68
- data/features/formatters/json_formatter.feature +0 -30
- data/features/formatters/regression_tests.feature +0 -95
- data/features/formatters/text_formatter.feature +0 -46
- data/features/helper_methods/arbitrary_methods.feature +0 -40
- data/features/helper_methods/let.feature +0 -50
- data/features/helper_methods/modules.feature +0 -146
- data/features/hooks/around_hooks.feature +0 -344
- data/features/hooks/before_and_after_hooks.feature +0 -427
- data/features/hooks/filtering.feature +0 -232
- data/features/metadata/current_example.feature +0 -56
- data/features/metadata/described_class.feature +0 -17
- data/features/metadata/user_defined.feature +0 -100
- data/features/mock_framework_integration/use_any_framework.feature +0 -106
- data/features/mock_framework_integration/use_flexmock.feature +0 -94
- data/features/mock_framework_integration/use_mocha.feature +0 -95
- data/features/mock_framework_integration/use_rr.feature +0 -96
- data/features/mock_framework_integration/use_rspec.feature +0 -95
- data/features/pending_and_skipped_examples/README.md +0 -3
- data/features/pending_and_skipped_examples/pending_examples.feature +0 -118
- data/features/pending_and_skipped_examples/skipped_examples.feature +0 -106
- data/features/spec_files/arbitrary_file_suffix.feature +0 -13
- data/features/step_definitions/additional_cli_steps.rb +0 -83
- data/features/subject/explicit_subject.feature +0 -101
- data/features/subject/implicit_subject.feature +0 -63
- data/features/subject/one_liner_syntax.feature +0 -71
- data/features/support/env.rb +0 -21
- data/features/support/require_expect_syntax_in_aruba_specs.rb +0 -16
- data/features/support/rubinius.rb +0 -6
- data/lib/rspec/core/command_line.rb +0 -35
- data/lib/rspec/core/drb_command_line.rb +0 -26
- data/lib/rspec/core/drb_options.rb +0 -87
- data/lib/rspec/core/formatters/legacy_formatter.rb +0 -227
- data/lib/rspec/core/shared_example_group/collection.rb +0 -27
- data/spec/command_line/order_spec.rb +0 -211
- data/spec/rspec/core/backtrace_formatter_spec.rb +0 -230
- data/spec/rspec/core/command_line_spec.rb +0 -112
- data/spec/rspec/core/command_line_spec_output.txt +0 -0
- data/spec/rspec/core/configuration_options_spec.rb +0 -409
- data/spec/rspec/core/configuration_spec.rb +0 -1479
- data/spec/rspec/core/drb_command_line_spec.rb +0 -102
- data/spec/rspec/core/drb_options_spec.rb +0 -193
- data/spec/rspec/core/dsl_spec.rb +0 -88
- data/spec/rspec/core/example_group_spec.rb +0 -1533
- data/spec/rspec/core/example_spec.rb +0 -642
- data/spec/rspec/core/filter_manager_spec.rb +0 -229
- data/spec/rspec/core/formatters/base_formatter_spec.rb +0 -64
- data/spec/rspec/core/formatters/base_text_formatter_spec.rb +0 -303
- data/spec/rspec/core/formatters/deprecation_formatter_spec.rb +0 -208
- data/spec/rspec/core/formatters/documentation_formatter_spec.rb +0 -75
- data/spec/rspec/core/formatters/helpers_spec.rb +0 -104
- data/spec/rspec/core/formatters/html_formatted-2.1.0.html +0 -392
- data/spec/rspec/core/formatters/html_formatted.html +0 -397
- data/spec/rspec/core/formatters/html_formatter_spec.rb +0 -122
- data/spec/rspec/core/formatters/json_formatter_spec.rb +0 -206
- data/spec/rspec/core/formatters/legacy_formatter_spec.rb +0 -137
- data/spec/rspec/core/formatters/progress_formatter_spec.rb +0 -43
- data/spec/rspec/core/formatters/snippet_extractor_spec.rb +0 -26
- data/spec/rspec/core/formatters_spec.rb +0 -120
- data/spec/rspec/core/hooks_filtering_spec.rb +0 -227
- data/spec/rspec/core/hooks_spec.rb +0 -294
- data/spec/rspec/core/memoized_helpers_spec.rb +0 -495
- data/spec/rspec/core/metadata_spec.rb +0 -491
- data/spec/rspec/core/option_parser_spec.rb +0 -262
- data/spec/rspec/core/ordering_spec.rb +0 -102
- data/spec/rspec/core/pending_example_spec.rb +0 -117
- data/spec/rspec/core/pending_spec.rb +0 -8
- data/spec/rspec/core/project_initializer_spec.rb +0 -73
- data/spec/rspec/core/rake_task_spec.rb +0 -146
- data/spec/rspec/core/random_spec.rb +0 -47
- data/spec/rspec/core/reporter_spec.rb +0 -155
- 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 +0 -1
- data/spec/rspec/core/resources/custom_example_group_runner.rb +0 -14
- data/spec/rspec/core/resources/formatter_specs.rb +0 -58
- data/spec/rspec/core/resources/utf8_encoded.rb +0 -8
- data/spec/rspec/core/rspec_matchers_spec.rb +0 -45
- data/spec/rspec/core/ruby_project_spec.rb +0 -26
- data/spec/rspec/core/runner_spec.rb +0 -151
- data/spec/rspec/core/shared_context_spec.rb +0 -102
- data/spec/rspec/core/shared_example_group/collection_spec.rb +0 -57
- data/spec/rspec/core/shared_example_group_spec.rb +0 -114
- data/spec/rspec/core/warnings_spec.rb +0 -29
- data/spec/rspec/core/world_spec.rb +0 -142
- data/spec/rspec/core_spec.rb +0 -91
- data/spec/spec_helper.rb +0 -160
- data/spec/support/config_options_helper.rb +0 -13
- data/spec/support/formatter_support.rb +0 -83
- data/spec/support/helper_methods.rb +0 -26
- data/spec/support/isolate_load_path_mutation.rb +0 -5
- data/spec/support/isolated_directory.rb +0 -10
- data/spec/support/isolated_home_directory.rb +0 -16
- data/spec/support/legacy_formatter_using_sub_classing_example.rb +0 -87
- data/spec/support/matchers.rb +0 -85
- data/spec/support/mathn_integration_support.rb +0 -12
- data/spec/support/old_style_formatter_example.rb +0 -69
- data/spec/support/shared_example_groups.rb +0 -13
- data/spec/support/spec_files.rb +0 -44
- data/spec/support/stderr_splitter.rb +0 -36
@@ -1,76 +0,0 @@
|
|
1
|
-
Feature: run all when everything filtered
|
2
|
-
|
3
|
-
Use the run_all_when_everything_filtered option to tell RSpec to run all the
|
4
|
-
specs in the case where you try to run a filtered list of specs but no specs
|
5
|
-
match that filter. This works well when paired with an inclusion filter like
|
6
|
-
`:focus => true`, as it will run all the examples when none match the
|
7
|
-
inclusion filter.
|
8
|
-
|
9
|
-
RSpec.configure { |c| c.run_all_when_everything_filtered = true }
|
10
|
-
|
11
|
-
Background:
|
12
|
-
Given a file named "spec/spec_helper.rb" with:
|
13
|
-
"""ruby
|
14
|
-
RSpec.configure {|c| c.run_all_when_everything_filtered = true}
|
15
|
-
"""
|
16
|
-
|
17
|
-
Scenario: by default, no specs are run if they are all filtered out by an inclusion tag
|
18
|
-
Given a file named "spec/example_spec.rb" with:
|
19
|
-
"""ruby
|
20
|
-
describe "examples" do
|
21
|
-
it "with no tag" do
|
22
|
-
end
|
23
|
-
|
24
|
-
it "with no tag as well" do
|
25
|
-
end
|
26
|
-
end
|
27
|
-
"""
|
28
|
-
When I run `rspec spec/example_spec.rb --tag some_tag`
|
29
|
-
Then the output should contain "0 examples, 0 failures"
|
30
|
-
|
31
|
-
Scenario: specs are still run if they are filtered out by an exclusion tag
|
32
|
-
Given a file named "spec/example_spec.rb" with:
|
33
|
-
"""ruby
|
34
|
-
describe "examples" do
|
35
|
-
it "with no tag" do
|
36
|
-
end
|
37
|
-
|
38
|
-
it "with no tag as well" do
|
39
|
-
end
|
40
|
-
end
|
41
|
-
"""
|
42
|
-
When I run `rspec spec/example_spec.rb --tag ~some_tag`
|
43
|
-
Then the output should contain "2 examples, 0 failures"
|
44
|
-
|
45
|
-
Scenario: when the run_all_when_everything_filtered option is turned on, if there are any matches for the filtering tag, only those features are run
|
46
|
-
Given a file named "spec/example_spec.rb" with:
|
47
|
-
"""ruby
|
48
|
-
require "spec_helper"
|
49
|
-
describe "examples" do
|
50
|
-
it "with no tag", :some_tag => true do
|
51
|
-
end
|
52
|
-
|
53
|
-
it "with no tag as well" do
|
54
|
-
end
|
55
|
-
end
|
56
|
-
"""
|
57
|
-
When I run `rspec spec/example_spec.rb --tag some_tag`
|
58
|
-
Then the output should contain "1 example, 0 failures"
|
59
|
-
And the output should contain "Run options: include {:some_tag=>true}"
|
60
|
-
|
61
|
-
Scenario: when the run_all_when_everything_filtered option is turned on, all the specs are run when the tag has no matches
|
62
|
-
Given a file named "spec/example_spec.rb" with:
|
63
|
-
"""ruby
|
64
|
-
require "spec_helper"
|
65
|
-
describe "examples" do
|
66
|
-
it "with no tag" do
|
67
|
-
end
|
68
|
-
|
69
|
-
it "with no tag as well" do
|
70
|
-
end
|
71
|
-
end
|
72
|
-
"""
|
73
|
-
When I run `rspec spec/example_spec.rb --tag some_tag`
|
74
|
-
Then the output should contain "2 examples, 0 failures"
|
75
|
-
And the output should contain "All examples were filtered out; ignoring {:some_tag=>true}"
|
76
|
-
|
@@ -1,48 +0,0 @@
|
|
1
|
-
Feature: aliasing
|
2
|
-
|
3
|
-
`describe` and `context` are the default aliases for `example_group`.
|
4
|
-
You can define your own aliases for `example_group` and give those
|
5
|
-
custom aliases default metadata.
|
6
|
-
|
7
|
-
RSpec provides a few built-in aliases:
|
8
|
-
|
9
|
-
* `xdescribe` and `xcontext` add `:pending` metadata to the
|
10
|
-
example group in order to temporarily disable the examples.
|
11
|
-
* `fdescribe` and `fcontext` add `:focus` metadata to the
|
12
|
-
example group in order to make it easy to temporarily
|
13
|
-
focus the example group (when combined with `config.filter_run :focus`.)
|
14
|
-
|
15
|
-
Scenario: custom example group aliases with metadata
|
16
|
-
Given a file named "nested_example_group_aliases_spec.rb" with:
|
17
|
-
"""ruby
|
18
|
-
RSpec.configure do |c|
|
19
|
-
c.alias_example_group_to :detail, :detailed => true
|
20
|
-
end
|
21
|
-
|
22
|
-
RSpec.detail "a detail" do
|
23
|
-
it "can do some less important stuff" do
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
RSpec.describe "a thing" do
|
28
|
-
describe "in broad strokes" do
|
29
|
-
it "can do things" do
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
detail "something less important" do
|
34
|
-
it "can do an unimportant thing" do
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
"""
|
39
|
-
When I run `rspec nested_example_group_aliases_spec.rb --tag detailed -fdoc`
|
40
|
-
Then the output should contain:
|
41
|
-
"""
|
42
|
-
a detail
|
43
|
-
can do some less important stuff
|
44
|
-
|
45
|
-
a thing
|
46
|
-
something less important
|
47
|
-
"""
|
48
|
-
|
@@ -1,55 +0,0 @@
|
|
1
|
-
Feature: basic structure (describe/it)
|
2
|
-
|
3
|
-
RSpec is a DSL for creating executable examples of how code is expected to
|
4
|
-
behave, organized in groups. It uses the words "describe" and "it" so we can
|
5
|
-
express concepts like a conversation:
|
6
|
-
|
7
|
-
"Describe an account when it is first opened."
|
8
|
-
"It has a balance of zero."
|
9
|
-
|
10
|
-
The `describe` method creates an example group. Within the block passed to
|
11
|
-
`describe` you can declare nested groups using the `describe` or `context`
|
12
|
-
methods, or you can declare examples using the `it` or `specify` methods.
|
13
|
-
|
14
|
-
Under the hood, an example group is a class in which the block passed to
|
15
|
-
`describe` or `context` is evaluated. The blocks passed to `it` are evaluated
|
16
|
-
in the context of an _instance_ of that class.
|
17
|
-
|
18
|
-
Scenario: one group, one example
|
19
|
-
Given a file named "sample_spec.rb" with:
|
20
|
-
"""ruby
|
21
|
-
describe "something" do
|
22
|
-
it "does something" do
|
23
|
-
end
|
24
|
-
end
|
25
|
-
"""
|
26
|
-
When I run `rspec sample_spec.rb -fdoc`
|
27
|
-
Then the output should contain:
|
28
|
-
"""
|
29
|
-
something
|
30
|
-
does something
|
31
|
-
"""
|
32
|
-
|
33
|
-
Scenario: nested example groups (using context)
|
34
|
-
Given a file named "nested_example_groups_spec.rb" with:
|
35
|
-
"""ruby
|
36
|
-
describe "something" do
|
37
|
-
context "in one context" do
|
38
|
-
it "does one thing" do
|
39
|
-
end
|
40
|
-
end
|
41
|
-
context "in another context" do
|
42
|
-
it "does another thing" do
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
"""
|
47
|
-
When I run `rspec nested_example_groups_spec.rb -fdoc`
|
48
|
-
Then the output should contain:
|
49
|
-
"""
|
50
|
-
something
|
51
|
-
in one context
|
52
|
-
does one thing
|
53
|
-
in another context
|
54
|
-
does another thing
|
55
|
-
"""
|
@@ -1,74 +0,0 @@
|
|
1
|
-
Feature: shared context
|
2
|
-
|
3
|
-
Use `shared_context` to define a block that will be evaluated in the context
|
4
|
-
of example groups either explicitly, using `include_context`, or implicitly by
|
5
|
-
matching metadata.
|
6
|
-
|
7
|
-
Background:
|
8
|
-
Given a file named "shared_stuff.rb" with:
|
9
|
-
"""ruby
|
10
|
-
shared_context "shared stuff", :a => :b do
|
11
|
-
before { @some_var = :some_value }
|
12
|
-
def shared_method
|
13
|
-
"it works"
|
14
|
-
end
|
15
|
-
let(:shared_let) { {'arbitrary' => 'object'} }
|
16
|
-
subject do
|
17
|
-
'this is the subject'
|
18
|
-
end
|
19
|
-
end
|
20
|
-
"""
|
21
|
-
|
22
|
-
Scenario: declare shared context and include it with include_context
|
23
|
-
Given a file named "shared_context_example.rb" with:
|
24
|
-
"""ruby
|
25
|
-
require "./shared_stuff.rb"
|
26
|
-
|
27
|
-
describe "group that includes a shared context using 'include_context'" do
|
28
|
-
include_context "shared stuff"
|
29
|
-
|
30
|
-
it "has access to methods defined in shared context" do
|
31
|
-
expect(shared_method).to eq("it works")
|
32
|
-
end
|
33
|
-
|
34
|
-
it "has access to methods defined with let in shared context" do
|
35
|
-
expect(shared_let['arbitrary']).to eq('object')
|
36
|
-
end
|
37
|
-
|
38
|
-
it "runs the before hooks defined in the shared context" do
|
39
|
-
expect(@some_var).to be(:some_value)
|
40
|
-
end
|
41
|
-
|
42
|
-
it "accesses the subject defined in the shared context" do
|
43
|
-
expect(subject).to eq('this is the subject')
|
44
|
-
end
|
45
|
-
end
|
46
|
-
"""
|
47
|
-
When I run `rspec shared_context_example.rb`
|
48
|
-
Then the examples should all pass
|
49
|
-
|
50
|
-
Scenario: declare shared context and include it with metadata
|
51
|
-
Given a file named "shared_context_example.rb" with:
|
52
|
-
"""ruby
|
53
|
-
require "./shared_stuff.rb"
|
54
|
-
|
55
|
-
describe "group that includes a shared context using metadata", :a => :b do
|
56
|
-
it "has access to methods defined in shared context" do
|
57
|
-
expect(shared_method).to eq("it works")
|
58
|
-
end
|
59
|
-
|
60
|
-
it "has access to methods defined with let in shared context" do
|
61
|
-
expect(shared_let['arbitrary']).to eq('object')
|
62
|
-
end
|
63
|
-
|
64
|
-
it "runs the before hooks defined in the shared context" do
|
65
|
-
expect(@some_var).to be(:some_value)
|
66
|
-
end
|
67
|
-
|
68
|
-
it "accesses the subject defined in the shared context" do
|
69
|
-
expect(subject).to eq('this is the subject')
|
70
|
-
end
|
71
|
-
end
|
72
|
-
"""
|
73
|
-
When I run `rspec shared_context_example.rb`
|
74
|
-
Then the examples should all pass
|
@@ -1,286 +0,0 @@
|
|
1
|
-
Feature: shared examples
|
2
|
-
|
3
|
-
Shared examples let you describe behaviour of types or modules. When
|
4
|
-
declared, a shared group's content is stored. It is only realized in the
|
5
|
-
context of another example group, which provides any context the shared group
|
6
|
-
needs to run.
|
7
|
-
|
8
|
-
A shared group is included in another group using any of:
|
9
|
-
|
10
|
-
include_examples "name" # include the examples in the current context
|
11
|
-
it_behaves_like "name" # include the examples in a nested context
|
12
|
-
it_should_behave_like "name" # include the examples in a nested context
|
13
|
-
matching metadata # include the examples in the current context
|
14
|
-
|
15
|
-
WARNING: Files containing shared groups must be loaded before the files that
|
16
|
-
use them. While there are conventions to handle this, RSpec does _not_ do
|
17
|
-
anything special (like autoload). Doing so would require a strict naming
|
18
|
-
convention for files that would break existing suites.
|
19
|
-
|
20
|
-
CONVENTIONS:
|
21
|
-
|
22
|
-
1. The simplest approach is to require files with shared examples explicitly
|
23
|
-
from the files that use them. Keep in mind that RSpec adds the `spec`
|
24
|
-
directory to the `LOAD_PATH`, so you can say `require
|
25
|
-
'shared_examples_for_widgets'` to require a file at
|
26
|
-
`#{PROJECT_ROOT}/spec/shared_examples_for_widgets.rb`.
|
27
|
-
|
28
|
-
2. Put files containing shared examples in `spec/support/` and require files
|
29
|
-
in that directory from `spec/spec_helper.rb`:
|
30
|
-
|
31
|
-
Dir["./spec/support/**/*.rb"].sort.each {|f| require f}
|
32
|
-
|
33
|
-
This is included in the generated `spec/spec_helper.rb` file in
|
34
|
-
`rspec-rails`
|
35
|
-
|
36
|
-
3. When all of the groups that include the shared group, just declare
|
37
|
-
the shared group in the same file.
|
38
|
-
|
39
|
-
Scenario: shared examples group included in two groups in one file
|
40
|
-
Given a file named "collection_spec.rb" with:
|
41
|
-
"""ruby
|
42
|
-
require "set"
|
43
|
-
|
44
|
-
shared_examples "a collection" do
|
45
|
-
let(:collection) { described_class.new([7, 2, 4]) }
|
46
|
-
|
47
|
-
context "initialized with 3 items" do
|
48
|
-
it "says it has three items" do
|
49
|
-
expect(collection.size).to eq(3)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe "#include?" do
|
54
|
-
context "with an an item that is in the collection" do
|
55
|
-
it "returns true" do
|
56
|
-
expect(collection.include?(7)).to be_truthy
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
context "with an an item that is not in the collection" do
|
61
|
-
it "returns false" do
|
62
|
-
expect(collection.include?(9)).to be_falsey
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
describe Array do
|
69
|
-
it_behaves_like "a collection"
|
70
|
-
end
|
71
|
-
|
72
|
-
describe Set do
|
73
|
-
it_behaves_like "a collection"
|
74
|
-
end
|
75
|
-
"""
|
76
|
-
When I run `rspec collection_spec.rb --format documentation`
|
77
|
-
Then the examples should all pass
|
78
|
-
And the output should contain:
|
79
|
-
"""
|
80
|
-
Array
|
81
|
-
behaves like a collection
|
82
|
-
initialized with 3 items
|
83
|
-
says it has three items
|
84
|
-
#include?
|
85
|
-
with an an item that is in the collection
|
86
|
-
returns true
|
87
|
-
with an an item that is not in the collection
|
88
|
-
returns false
|
89
|
-
|
90
|
-
Set
|
91
|
-
behaves like a collection
|
92
|
-
initialized with 3 items
|
93
|
-
says it has three items
|
94
|
-
#include?
|
95
|
-
with an an item that is in the collection
|
96
|
-
returns true
|
97
|
-
with an an item that is not in the collection
|
98
|
-
returns false
|
99
|
-
"""
|
100
|
-
|
101
|
-
Scenario: Providing context to a shared group using a block
|
102
|
-
Given a file named "shared_example_group_spec.rb" with:
|
103
|
-
"""ruby
|
104
|
-
require "set"
|
105
|
-
|
106
|
-
shared_examples "a collection object" do
|
107
|
-
describe "<<" do
|
108
|
-
it "adds objects to the end of the collection" do
|
109
|
-
collection << 1
|
110
|
-
collection << 2
|
111
|
-
expect(collection.to_a).to match_array([1, 2])
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
describe Array do
|
117
|
-
it_behaves_like "a collection object" do
|
118
|
-
let(:collection) { Array.new }
|
119
|
-
end
|
120
|
-
end
|
121
|
-
|
122
|
-
describe Set do
|
123
|
-
it_behaves_like "a collection object" do
|
124
|
-
let(:collection) { Set.new }
|
125
|
-
end
|
126
|
-
end
|
127
|
-
"""
|
128
|
-
When I run `rspec shared_example_group_spec.rb --format documentation`
|
129
|
-
Then the examples should all pass
|
130
|
-
And the output should contain:
|
131
|
-
"""
|
132
|
-
Array
|
133
|
-
behaves like a collection object
|
134
|
-
<<
|
135
|
-
adds objects to the end of the collection
|
136
|
-
|
137
|
-
Set
|
138
|
-
behaves like a collection object
|
139
|
-
<<
|
140
|
-
adds objects to the end of the collection
|
141
|
-
"""
|
142
|
-
|
143
|
-
Scenario: Passing parameters to a shared example group
|
144
|
-
Given a file named "shared_example_group_params_spec.rb" with:
|
145
|
-
"""ruby
|
146
|
-
shared_examples "a measurable object" do |measurement, measurement_methods|
|
147
|
-
measurement_methods.each do |measurement_method|
|
148
|
-
it "should return #{measurement} from ##{measurement_method}" do
|
149
|
-
expect(subject.send(measurement_method)).to eq(measurement)
|
150
|
-
end
|
151
|
-
end
|
152
|
-
end
|
153
|
-
|
154
|
-
describe Array, "with 3 items" do
|
155
|
-
subject { [1, 2, 3] }
|
156
|
-
it_should_behave_like "a measurable object", 3, [:size, :length]
|
157
|
-
end
|
158
|
-
|
159
|
-
describe String, "of 6 characters" do
|
160
|
-
subject { "FooBar" }
|
161
|
-
it_should_behave_like "a measurable object", 6, [:size, :length]
|
162
|
-
end
|
163
|
-
"""
|
164
|
-
When I run `rspec shared_example_group_params_spec.rb --format documentation`
|
165
|
-
Then the examples should all pass
|
166
|
-
And the output should contain:
|
167
|
-
"""
|
168
|
-
Array with 3 items
|
169
|
-
it should behave like a measurable object
|
170
|
-
should return 3 from #size
|
171
|
-
should return 3 from #length
|
172
|
-
|
173
|
-
String of 6 characters
|
174
|
-
it should behave like a measurable object
|
175
|
-
should return 6 from #size
|
176
|
-
should return 6 from #length
|
177
|
-
"""
|
178
|
-
|
179
|
-
Scenario: Aliasing "it_should_behave_like" to "it_has_behavior"
|
180
|
-
Given a file named "shared_example_group_spec.rb" with:
|
181
|
-
"""ruby
|
182
|
-
RSpec.configure do |c|
|
183
|
-
c.alias_it_should_behave_like_to :it_has_behavior, 'has behavior:'
|
184
|
-
end
|
185
|
-
|
186
|
-
shared_examples 'sortability' do
|
187
|
-
it 'responds to <=>' do
|
188
|
-
expect(sortable).to respond_to(:<=>)
|
189
|
-
end
|
190
|
-
end
|
191
|
-
|
192
|
-
describe String do
|
193
|
-
it_has_behavior 'sortability' do
|
194
|
-
let(:sortable) { 'sample string' }
|
195
|
-
end
|
196
|
-
end
|
197
|
-
"""
|
198
|
-
When I run `rspec shared_example_group_spec.rb --format documentation`
|
199
|
-
Then the examples should all pass
|
200
|
-
And the output should contain:
|
201
|
-
"""
|
202
|
-
String
|
203
|
-
has behavior: sortability
|
204
|
-
responds to <=>
|
205
|
-
"""
|
206
|
-
|
207
|
-
Scenario: Sharing metadata automatically includes shared example groups
|
208
|
-
Given a file named "shared_example_metadata_spec.rb" with:
|
209
|
-
"""ruby
|
210
|
-
shared_examples "shared stuff", :a => :b do
|
211
|
-
it 'runs wherever the metadata is shared' do
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
describe String, :a => :b do
|
216
|
-
end
|
217
|
-
"""
|
218
|
-
When I run `rspec shared_example_metadata_spec.rb`
|
219
|
-
Then the output should contain:
|
220
|
-
"""
|
221
|
-
1 example, 0 failures
|
222
|
-
"""
|
223
|
-
|
224
|
-
Scenario: Shared examples are nestable by context
|
225
|
-
Given a file named "context_specific_examples_spec.rb" with:
|
226
|
-
"""Ruby
|
227
|
-
describe "shared examples" do
|
228
|
-
context "per context" do
|
229
|
-
|
230
|
-
shared_examples "shared examples are nestable" do
|
231
|
-
specify { expect(true).to eq true }
|
232
|
-
end
|
233
|
-
|
234
|
-
it_behaves_like "shared examples are nestable"
|
235
|
-
end
|
236
|
-
end
|
237
|
-
"""
|
238
|
-
When I run `rspec context_specific_examples_spec.rb`
|
239
|
-
Then the output should contain:
|
240
|
-
"""
|
241
|
-
1 example, 0 failures
|
242
|
-
"""
|
243
|
-
|
244
|
-
Scenario: Shared examples are accessible from offspring contexts
|
245
|
-
Given a file named "context_specific_examples_spec.rb" with:
|
246
|
-
"""Ruby
|
247
|
-
describe "shared examples" do
|
248
|
-
shared_examples "shared examples are nestable" do
|
249
|
-
specify { expect(true).to eq true }
|
250
|
-
end
|
251
|
-
|
252
|
-
context "per context" do
|
253
|
-
it_behaves_like "shared examples are nestable"
|
254
|
-
end
|
255
|
-
end
|
256
|
-
"""
|
257
|
-
When I run `rspec context_specific_examples_spec.rb`
|
258
|
-
Then the output should contain:
|
259
|
-
"""
|
260
|
-
1 example, 0 failures
|
261
|
-
"""
|
262
|
-
And the output should not contain:
|
263
|
-
"""
|
264
|
-
Accessing shared_examples defined across contexts is deprecated
|
265
|
-
"""
|
266
|
-
|
267
|
-
Scenario: Shared examples are isolated per context
|
268
|
-
Given a file named "isolated_shared_examples_spec.rb" with:
|
269
|
-
"""Ruby
|
270
|
-
describe "shared examples" do
|
271
|
-
context do
|
272
|
-
shared_examples "shared examples are isolated" do
|
273
|
-
specify { expect(true).to eq true }
|
274
|
-
end
|
275
|
-
end
|
276
|
-
|
277
|
-
context do
|
278
|
-
it_behaves_like "shared examples are isolated"
|
279
|
-
end
|
280
|
-
end
|
281
|
-
"""
|
282
|
-
When I run `rspec isolated_shared_examples_spec.rb`
|
283
|
-
Then the output should contain:
|
284
|
-
"""
|
285
|
-
Could not find shared examples \"shared examples are isolated\"
|
286
|
-
"""
|