Reputation: 4753
I am trying to learn Rspec. My ruby project in eclipse is as follows -
The code-
require 'rspec'
require './RubyOffRailsTuts/classes/furlong'
describe Furlong do
end
The error-
/RubyOffRailsTuts/specs/furlong_spec.rb:6:in `<main>': undefined
method `describe' for main:Object (NoMethodError)
Did not get any useful answers online. How do I fix this problem ?
Upvotes: 37
Views: 27966
Reputation: 13715
Call RSpec.describe
instead of just describe
.
require 'rspec'
require './RubyOffRailsTuts/classes/furlong'
RSpec.describe Furlong do
end
You can tell from the error message "undefined method `describe' for main Object" that the underlying problem is that you are trying to call describe
on the the basic Object main
, which does not have a describe
method.
Upvotes: 43
Reputation: 901
If someone by any chance gets a similar brain blackout for couple minutes like myself a while ago and does use:
ruby spec/yourspec.rb
instead of
rspec spec/yourspec.rb
And is completely stunned why this did work a minute ago and does not right now when nothing has changed - this is exactly the error that pops in.
Upvotes: 2
Reputation: 11988
I agree with sevenseacat that you're likely using a modern version of RSpec that disables monkey patching.
This disabling is done by default when the spec_helper.rb
file is created when you do something like
$ rails generate rspec:install
In spec_helper.rb
, you'll see a section that looks like this:
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
config.disable_monkey_patching!
You can comment out that last line.
However, the recommended approach is not to use monkey patching, and use RSpec.describe
.
Upvotes: 20
Reputation: 1141
Alternative to prefacing describe
as RSpec.describe
, you can add
config.expose_dsl_globally = true
to your spec_helper.rb
.
Upvotes: 51
Reputation: 25049
You prefix describe
with RSpec
, eg. RSpec.describe
because it sounds like you're using a modern version of RSpec that disables monkey patching.
Upvotes: 23