RSpec NoMethodError:“未定义的方法”描述“主要对象”

时间:2021-06-25 23:52:40

I am trying to learn Rspec. My ruby project in eclipse is as follows -

我正在努力学习Rspec。我在eclipse中的ruby项目如下

RSpec NoMethodError:“未定义的方法”描述“主要对象”

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 ?

在网上没有得到任何有用的答案。我如何解决这个问题?

4 个解决方案

#1


28  

Alternative to prefacing describe as RSpec.describe, you can add

描述,你可以添加。

config.expose_dsl_globally = true

to your spec_helper.rb.

spec_helper.rb。

#2


20  

The underlying problem is the basic Object main does not have a describe method unless you give it one, which is reflected by the error message "undefined method describe for main Object".

基本问题是,基本对象main没有描述方法,除非您给它一个,这反映在“未定义的方法描述主对象”的错误消息中。

Offhand, I can think of two ways to fix this:

我可以马上想出两种方法来解决这个问题:

1) Call RSpec.describe instead of just describe

1)打电话询问,描述而不是描述

require 'rspec'
require './RubyOffRailsTuts/classes/furlong'

RSpec.describe Furlong do
end

2) Call include RSpec to make describe available to main

2)调用包括RSpec,使描述可用到main。

require 'rspec'
require './RubyOffRailsTuts/classes/furlong'

include RSpec

describe Furlong do
end

#3


16  

You prefix describe with RSpec, eg. RSpec.describe because it sounds like you're using a modern version of RSpec that disables monkey patching.

你的前缀用RSpec来描述。描述,因为听起来你在使用一个现代版本的RSpec,它禁止猴子修补。

#4


13  

I agree with sevenseacat that you're likely using a modern version of RSpec that disables monkey patching.

我同意sevenseacat的观点,您可能使用了一个现代版本的RSpec,它禁止monkey patching。

This disabling is done by default when the spec_helper.rb file is created when you do something like

默认情况下,当spec_helper函数执行此禁用操作。当您执行以下操作时,将创建rb文件

$ rails generate rspec:install

In spec_helper.rb, you'll see a section that looks like this:

在spec_helper。rb,你会看到一个像这样的部分:

# 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.

但是,推荐的方法不是使用monkey patching,而是使用RSpec.describe。

#1


28  

Alternative to prefacing describe as RSpec.describe, you can add

描述,你可以添加。

config.expose_dsl_globally = true

to your spec_helper.rb.

spec_helper.rb。

#2


20  

The underlying problem is the basic Object main does not have a describe method unless you give it one, which is reflected by the error message "undefined method describe for main Object".

基本问题是,基本对象main没有描述方法,除非您给它一个,这反映在“未定义的方法描述主对象”的错误消息中。

Offhand, I can think of two ways to fix this:

我可以马上想出两种方法来解决这个问题:

1) Call RSpec.describe instead of just describe

1)打电话询问,描述而不是描述

require 'rspec'
require './RubyOffRailsTuts/classes/furlong'

RSpec.describe Furlong do
end

2) Call include RSpec to make describe available to main

2)调用包括RSpec,使描述可用到main。

require 'rspec'
require './RubyOffRailsTuts/classes/furlong'

include RSpec

describe Furlong do
end

#3


16  

You prefix describe with RSpec, eg. RSpec.describe because it sounds like you're using a modern version of RSpec that disables monkey patching.

你的前缀用RSpec来描述。描述,因为听起来你在使用一个现代版本的RSpec,它禁止猴子修补。

#4


13  

I agree with sevenseacat that you're likely using a modern version of RSpec that disables monkey patching.

我同意sevenseacat的观点,您可能使用了一个现代版本的RSpec,它禁止monkey patching。

This disabling is done by default when the spec_helper.rb file is created when you do something like

默认情况下,当spec_helper函数执行此禁用操作。当您执行以下操作时,将创建rb文件

$ rails generate rspec:install

In spec_helper.rb, you'll see a section that looks like this:

在spec_helper。rb,你会看到一个像这样的部分:

# 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.

但是,推荐的方法不是使用monkey patching,而是使用RSpec.describe。