Skip to content

Instantly share code, notes, and snippets.

@kyamaguchi
Last active August 29, 2015 14:19
Show Gist options
  • Save kyamaguchi/b59e5c74b768975c6309 to your computer and use it in GitHub Desktop.
Save kyamaguchi/b59e5c74b768975c6309 to your computer and use it in GitHub Desktop.
Test output of rerun commands of shared_examples_for
require 'spec_helper'
# Version: rspec (3.2.0)
RSpec.describe "Rerun commands output" do
shared_examples_for "random" do
it "more than 3" do
expect(rand(10)).to be > 3
end
it "more than 5" do
expect(rand(10)).to be > 5
end
end
context "main case 1" do
it_should_behave_like "random"
end
context "main case 2" do
it "using it" do
expect(rand(10)).to be > 6
end
it_should_behave_like "random"
end
end
=begin
$ rspec spec/rerun_commands_output_spec.rb
.FFF.
Failures:
1) Rerun commands output main case 1 it should behave like random more than 5
Failure/Error: expect(rand(10)).to be > 5
expected: > 5
got: 0
Shared Example Group: "random" called from ./spec/rerun_commands_output_spec.rb:16
# ./spec/rerun_commands_output_spec.rb:11:in `block (3 levels) in <top (required)>'
2) Rerun commands output main case 2 using it
Failure/Error: expect(rand(10)).to be > 6
expected: > 6
got: 0
# ./spec/rerun_commands_output_spec.rb:21:in `block (3 levels) in <top (required)>'
3) Rerun commands output main case 2 it should behave like random more than 3
Failure/Error: expect(rand(10)).to be > 3
expected: > 3
got: 2
Shared Example Group: "random" called from ./spec/rerun_commands_output_spec.rb:24
# ./spec/rerun_commands_output_spec.rb:7:in `block (3 levels) in <top (required)>'
Finished in 0.00236 seconds (files took 0.16156 seconds to load)
5 examples, 3 failures
Failed examples:
rspec ./spec/rerun_commands_output_spec.rb:10 # Rerun commands output main case 1 it should behave like random more than 5
rspec ./spec/rerun_commands_output_spec.rb:20 # Rerun commands output main case 2 using it
rspec ./spec/rerun_commands_output_spec.rb:6 # Rerun commands output main case 2 it should behave like random more than 3
=end
## My expectation
=begin
Failed examples:
rspec ./spec/rerun_commands_output_spec.rb:16 # Rerun commands output main case 1 it should behave like random more than 5
rspec ./spec/rerun_commands_output_spec.rb:20 # Rerun commands output main case 2 using it
rspec ./spec/rerun_commands_output_spec.rb:24 # Rerun commands output main case 2 it should behave like random more than 3
=end
@myronmarston
Copy link

Yep, you've discovered one of the problems with using file location to identify examples: one location can match multiple examples. In your example, both solutions would match two examples -- as it is done now, it would match one of the shared examples for both inclusion sites, or if we did what you want, it would match all examples from the shared group at one inclusion site. Actually, if you move the shared group definition out of a spec file and into a support file, it would produce the output you want, because RSpec can't use the support file in the re-run command since it's not a spec file.

In RSpec 3.3 we have an even better solution coming: examples are all now identified by a unique id. When I run the above example against RSpec HEAD, I get this output:

rspec './spec/foo_spec.rb[1:1:1:2]' # Rerun commands output main case 1 it should behave like random more than 5
rspec ./spec/foo_spec.rb:20 # Rerun commands output main case 2 using it
rspec './spec/foo_spec.rb[1:2:2:1]' # Rerun commands output main case 2 it should behave like random more than 3

[1:1:1:2] and [1:2:2:1] are example ids. The first identifies the 2nd example in the 1st nested group within the 1st nested group within the 1st top level group. The 2nd identifies the 1st example in the 2nd nested group within the 2nd nested group within the 1st top level group. RSpec 3.3. will emit IDs instead of a location for the rerun command when there is no unique location that identifies the example.

There's also some new features coming that you may find more useful than using the rerun commands, anyway:

https://github.com/rspec/rspec-core/blob/master/features/command_line/only_failures.feature

On a side note, your use of randomization means that your example produces different output anytime. I tweaked it so that it is deterministic but causes the same failures:

RSpec.describe "Rerun commands output" do

  shared_examples_for "random" do |fail_for_1, fail_for_2|
    it "more than 3" do
      expect(1).to eq(2) if fail_for_1
    end

    it "more than 5" do
      expect(1).to eq(2) if fail_for_2
    end
  end

  context "main case 1" do
    it_should_behave_like "random", false, true
  end

  context "main case 2" do
    it "using it" do
      expect(1).to eq(2)
    end

    it_should_behave_like "random", true, false
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment