Why when uncommenting "config.disable_monkey_patching!" RSpec prefix is mandadory ?

63 views
Skip to first unread message

Javix

unread,
Sep 21, 2017, 10:44:37 AM9/21/17
to rspec
I discovered that when I uncomment begin /end block in the generated spec_helper.rb file my simple spec fails if I do not use RSpec prefix for describe blocks as follows:

require 'rack/test'


module ExpenseTracker
  describe
'API draft' do
    include
Rack::Test::Methods
   
    it
'records submitted expenses'
 
end
end


Error:

An error occurred while loading ./spec/unit/draft_spec.rb.

Failure/Error:

  describe 'API draft' do

    include Rack::Test::Methods

    

    it 'records submitted expenses'

  end


NoMethodError:

  undefined method `describe' for ExpenseTracker:Module

# ./spec/unit/draft_spec.rb:4:in `<module:ExpenseTracker>'

# ./spec/unit/draft_spec.rb:3:in `<top (required)>'

No examples found.


Finished in 0.0003 seconds (files took 0.12244 seconds to load)

0 examples, 0 failures, 1 error occurred outside of examples


If I change the same spec by prefixing describe with RSpec, it works:

module ExpenseTracker
 
RSpec.describe 'API draft' do
    include
Rack::Test::Methods
   
    it
'records submitted expenses'
 
end
end

expense_tracker git:(unit_tests) rspec spec/unit/draft_spec.rb


API draft

  records submitted expenses (PENDING: Not yet implemented)


Pending: (Failures listed here are expected and do not affect your suite's status)


  1) API draft records submitted expenses

     # Not yet implemented

     # ./spec/unit/draft_spec.rb:7



Finished in 0.00194 seconds (files took 0.12484 seconds to load)

1 example, 0 failures, 1 pending


Any ideas why ? Thank you.

Myron Marston

unread,
Sep 21, 2017, 11:21:27 AM9/21/17
to rs...@googlegroups.com

For a bare top-level describe to work, RSpec has to monkey patch some objects it does not own (specifically, the main object, and the Module class). The config.disable_monkey_patching! option turns off all of RSpec’s monkey patching, including the describe monkey patched onto main and Module. Thus, when you use that option, you get a NoMethodError when you try to call Module#describe or main.describe as you are in your example.

For more info, check out our original discussion of the option. Also, if you’re going through the new Effective Testing with RSpec 3: Build Ruby Apps with Confidence book (as I suspect you are, given the book builds an app called ExpenseTracker and it looks like that’s what you’re working on!), there’s an explanation of this at the bottom of page 163 and top of page 164.

HTH,
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/05b92135-d300-4f40-98ea-e7725d6e9fba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Javix

unread,
Sep 21, 2017, 4:27:28 PM9/21/17
to rspec


On Thursday, 21 September 2017 17:21:27 UTC+2, Myron Marston wrote:

For a bare top-level describe to work, RSpec has to monkey patch some objects it does not own (specifically, the main object, and the Module class). The config.disable_monkey_patching! option turns off all of RSpec’s monkey patching, including the describe monkey patched onto main and Module. Thus, when you use that option, you get a NoMethodError when you try to call Module#describe or main.describe as you are in your example.

For more info, check out our original discussion of the option. Also, if you’re going through the new Effective Testing with RSpec 3: Build Ruby Apps with Confidence book (as I suspect you are, given the book builds an app called ExpenseTracker and it looks like that’s what you’re working on!), there’s an explanation of this at the bottom of page 163 and top of page 164.

HTH,
Myron


Thanks a lot, Myron !
Yes, you are absolutely right, I am going through the latest Spec book you mentioned and like it very much as the previous one by the way :).
Sure, I'll take a look the original discussion and the explanation on the page you mentioned.

All the best,

Serguei
 

To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages