Obromios
Obromios

Reputation: 16373

rspec not reporting line number of error

For almost all my specs, when rspec reports an error, it informs me of the line number at the end of the path e.g.

rspec ./spec/controllers/eclubs_controller_spec.rb:21

However in one of my specs, it reports the error location like this

rspec ./spec/controllers/eclubs/members_controller_spec.rb[1:1:2:3:1]

which may make sense in terms of the nesting of blocks but frankly is rather cryptic.

The top part of the spec that works looks like this

require 'rails_helper'
describe EclubsController do

and the one that does not work looks like this

require 'rails_helper'
describe Eclubs::MembersController do

The only difference I can see in the two files is that one controller is namespaced, but I have other namespaced controllers that report the error line correctly.

What is causing this?

Upvotes: 4

Views: 637

Answers (1)

wicz
wicz

Reputation: 2313

RSpec uses the example id when the line number is not sufficiently unique to identify the example in question.

This can happen when examples are dynamically defined, for example in a loop:

(0..10).each do |i|
  it do
    expect(i).to_not eq(5)
  end
end
# rspec './my_spec.rb[1:6]'

Or when using a shared example group:

RSpec.shared_examples_for "not equal 5" do |i|
  it do
    expect(i).to_not eq(5)
  end
end

RSpec.describe do
  it_behaves_like "not equal 5", 5
  it_behaves_like "not equal 5", 4
end
# rspec './my_spec.rb[2:1:1]'

Upvotes: 3

Related Questions