rspec run is a failure, but returns exit code 0

579 views
Skip to first unread message

JQN

unread,
Oct 5, 2017, 8:36:12 AM10/5/17
to rspec
When i run multiple specs using rspec and one of the examples is a failure, the exit code that is returned is 0 than 1, but rspec report is correct in reporting example failures.
Few articles mention that this is because of a bug in ruby and to add a monkey patch
e.g .http://www.davekonopka.com/2013/rspec-exit-code.html

but this does not work for me either. I would appreciate any help.

Regards

JQN

unread,
Oct 5, 2017, 9:29:41 AM10/5/17
to rspec
I just wanted to add that the test is a failure because the element that the 'expect' was looking for was not found so it is an error.

Myron Marston

unread,
Oct 5, 2017, 11:18:32 AM10/5/17
to rs...@googlegroups.com

RSpec, as of v3.0, no longer uses at_exit (unless you require rspec/autorun—but that’s not necessary if you’re using the rspec command), so I’m not surprised the monkey patch to at_exit doesn’t work to fix your problem.

RSpec sets the exit code here:

https://github.com/rspec/rspec-core/blob/v3.6.0/lib/rspec/core/runner.rb#L45-L46

You can try adding some puts statements to your local copy of the RSpec source to debug it (bundle show rspec-core should show you where it’s installed). My guess is that you have some code either in your project or in a dependency that also calls exit or exit!, or that calls at_exit and interferes with the exit code in some fashion. If you don’t find any code doing this, my suggestion is to apply a binary search technique to your codebase. Define a simple, small spec file that always fails like:

require 'spec_helper' # not needed if you have `--require spec_helper` in `.rspec`

RSpec.describe "Failure" do
  it "fails" do
    expect(1).to eq 2
  end
end

Run just that spec file and see what the exit code is. If it’s zero, it means it’s something loaded by spec_helper that’s interfering. Comment out half of spec_helper and repeat until you identify it. If it was non-zero, it means it’s something being loaded by your other spec files (or in one of the spec files) causing it. Try systematically loading (or deleting, assuming you are using source control) different halves of your spec files to see if you can identify where the issue is.

Please let us know if you find the issue, as it can help others in the future!

Myron


--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+unsubscribe@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/d196d5bf-b887-4625-a23e-58b7409cbfe9%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages