undefined method `detect' for nil:NilClass (NoMethodError) in `find_failed_line'

15 views
Skip to first unread message

Jack Royal-Gordon

unread,
Sep 5, 2020, 12:48:37 AM9/5/20
to rs...@googlegroups.com
Running RSpec 2.99 (I know, I know, but I’m working on bringing my project up to date), and one section of one of my spec files gives that error. I’m not necessarily looking for a fix as much as a strategy for troubleshooting. Each of the three examples by itself will cause the same problem (both by isolating them on the command line and by commenting out the code). I also tried eliminating the “before” block (and FWIW, the call being tested works fine in production, not that that really means anything).


The section in question looks like:

require 'spec_helper'

describe UserDistributor do
  describe '#scrape_sales_data' do
    let(:ud) { FactoryGirl.create :user_distributor, :nook }

    before do
      allow_any_instance_of(DistributorWorker).to receive(:scrape_sales_data_for_user).and_return(true)
    end
    
    subject { ud.scrape_sales_data }
    
    it 'sets the user scrape_status field' do
      expect { subject }.to change { ud.user.scrape_status }
    end
    
    it 'creates a distributor worker' do
      expect(ud.distributor).to receve(:worker)
      subject
    end
    
    it 'calls #scrape_sales_data_for_user on the distibutor worker with the UD' do
      expect_any_instance_of(DistributorWorker).to receive(:scrape_sales_data_for_user).with(ud)
      subject
    end
  end
end

I get the following output (using —format documentation and —backtrace):

UserDistributor
  #scrape_sales_data
    sets the user scrape_status field (FAILED - 1)
    creates a distributor worker (FAILED - 2)
    calls #scrape_sales_data_for_user on the distibutor worker with the UD (FAILED - 3)

Failures:

  1) UserDistributor#scrape_sales_data sets the user scrape_status field
/Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/formatters/base_formatter.rb:209:in `find_failed_line': undefined method `detect' for nil:NilClass (NoMethodError)
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/formatters/base_formatter.rb:191:in `read_failed_line'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/formatters/base_text_formatter.rb:300:in `dump_failure_info'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/formatters/base_text_formatter.rb:294:in `dump_failure'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/formatters/base_text_formatter.rb:24:in `block in dump_failures'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/formatters/base_text_formatter.rb:22:in `each'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/formatters/base_text_formatter.rb:22:in `each_with_index'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/formatters/base_text_formatter.rb:22:in `dump_failures'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/reporter.rb:130:in `block in notify'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/reporter.rb:129:in `each'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/reporter.rb:129:in `notify'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/reporter.rb:109:in `finish'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/reporter.rb:60:in `report'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/command_line.rb:21:in `run'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:103:in `run'
from /Users/jackrg/.rbenv/versions/2.0.0-p576/lib/ruby/gems/2.0.0/gems/rspec-core-2.99.2/lib/rspec/core/runner.rb:17:in `block in autorun'

Phil Pirozhkov

unread,
Sep 5, 2020, 3:50:23 AM9/5/20
to Jack Royal-Gordon
Hi Jack

Sorry for brevity, will reply in more detail later when I get to my computer.

One thing I could spot is a typo in "receve" in one of the examples.

There might be something wrong with the other examples, but I understand that you're looking for a generic solution how to quickly get what's wrong in a failure message, and clearly "undefined method" raised somewhere in RSpec's internals is nowhere near being helpful.

I guess ‎we'll have to dig whats in base_formatter.rb:209, that may give an idea of what is the root cause of missing exact failure messages. I'll check it out, but if you have a chance to take a look yourself earlier than I'll get to it, don't hesitate. RSpec is just as good as any other code, probably even easier to read and understand.

- Phil
--
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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/B695317A-868B-4DF9-829B-62D84B6C1AB5%40pobox.com.

Jack Royal-Gordon

unread,
Sep 5, 2020, 11:25:57 AM9/5/20
to rs...@googlegroups.com
Interestingly enough, that typo was apparently the proximate cause of the error - once I corrected the spelling of “receive” I then got a usable error message. So, Phil, Jon, does this warrant filing a bug, given that it’s in Version 2.99 of RSpec?

Thanks, Phil. I don’t know how many times I looked at that code without seeing the typo.

Jack

Phil Pirozhkov

unread,
Sep 6, 2020, 2:17:01 PM9/6/20
to Jack Royal-Gordon
does this warrant filing a bug, given that it’s in Version 2.99 of RSpec?

You're in the best position to do fix the issue with your current task at hand. If you're up for it and if the issue pops up again - sure, add a task, or just send a pull request.
 
Thanks, Phil. I don’t know how many times I looked at that code without seeing the typo.

You're welcome.
I have a good eye for it since I usually type it as `recieve`.

- Phil

Jon Rowe

unread,
Sep 9, 2020, 8:55:10 AM9/9/20
to rs...@googlegroups.com
Hi

If you find something you think fixable I’d happily take a look at it, but I’m not promising we’ll release it as a fix, 2.99 is intended to transition existing passing suites to 3.x and as such isn’t really supported.

That said if its an easy bug fix I don’t see the harm in releasing it either…

Cheers
Jon

Jack Royal-Gordon

unread,
Sep 9, 2020, 3:44:43 PM9/9/20
to rs...@googlegroups.com
Hi Jon,

It turns out that the real underlying reason for the failure was that I was generating an exception with no backtrace (backtrace = nil). Does that seem to you like something you would want RSpec to handle more gracefully, or is that one of those “if you make the program fool-proof then they’ll create a better fool” type of errors?

--
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+un...@googlegroups.com.

Jon Rowe

unread,
Sep 15, 2020, 6:23:19 PM9/15/20
to rs...@googlegroups.com
We have, er, “citations” for protecting against bad exceptions, in general we don’t protect against people not obeying Ruby semantics, but as exceptions are a core part of what we do, we protect against bad messages and causes, so I don’t see why we wouldn’t protect against bad stack traces either.

Reply all
Reply to author
Forward
0 new messages