未定义的方法`得到'为#<RSpec :: Core :: ExampleGroup :: Nested_1:0x00000106db51f8>

任何人都知道如何解决这个问题? 在OSX上,试图让Rails 3.0.7运行RSpec。 详细信息请参阅: https : //gist.github.com/1017044

it "renders buttons_widgets partial" do get :buttons_widgets response.should render_template("buttons_widgets") end → rspec tools_model_spec.rb /Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/version.rb:4: warning: already initialized constant STRING /Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/metadata.rb:48: warning: already initialized constant RESERVED_KEYS /Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/pending.rb:6: warning: already initialized constant DEFAULT_MESSAGE /Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/world.rb:6: warning: already initialized constant PROC_HEX_NUMBER /Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/world.rb:7: warning: already initialized constant PROJECT_DIR /Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/configuration.rb:43: warning: already initialized constant CONDITIONAL_FILTERS /Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/configuration.rb:48: warning: already initialized constant DEFAULT_BACKTRACE_PATTERNS /Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core/runner.rb:13: warning: already initialized constant AT_EXIT_HOOK_BACKTRACE_LINE /Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/bundler/gems/rspec-core-bea2366c817e/lib/rspec/core.rb:35: warning: already initialized constant SharedContext Run filtered excluding {:if=>#<Proc:/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:43>, :unless=>#<Proc:/Users/mm/.rvm/gems/ruby-1.9.2-p0@evergreen/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:44>} F Failures: 1) ToolsController renders buttons_widgets partial Failure/Error: get :buttons_widgets NoMethodError: undefined method `get' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000106db51f8> # ./tools_model_spec.rb:7:in `block (2 levels) in <top (required)>' 

RSpec不知道你的规范是一个控制器规范,所以你的例子不能访问get方法。

RSpec 2.x假定控制器目录中的所有内容都是控制器规范。

这在RSpec 3中被改变了:

文件types推断默认是禁用的

以前,我们自动从文件位置推断spectypes,这对于新用户来说是一个令人惊讶的行为,对于一些资深用户来说是不可取的,所以从RSpec 3开始,这个行为必须明确地join到:

 RSpec.configure do |config| config.infer_spec_type_from_file_location! end 

https://www.relishapp.com/rspec/rspec-rails/docs/upgrade#file-type-in​​ference-disabled

在rspec-rails自述文件中 :

控制器规格默认存在于spec/controllers folder 。 使用元数据标记任何上下文:type => :controller将其作为控制器规范的示例。

为RSpec设置控制器上下文元数据的示例:

 describe ToolsController, :type => :controller do # ... end 

如果您正在使用“spec / features”,则可能需要将以下内容添加到“spec_helper.rb”

 config.include RSpec::Rails::RequestExampleGroup, type: :feature 

在Rspec 3.x中,spectypes不是从文件位置自动推断出来的,你必须手动设置它,把它添加到spec_helper.rb

 RSpec.configure do |config| config.infer_spec_type_from_file_location! end 

Rspec升级

对于其他人来看这个。 我试图追查一个undefined method 'get'错误。 我的问题是我get了一个describe block确保你get一个it block

我能够通过在我的spec_helper文件中添加require'rspec require 'rspec/rails'来解决这个问题。

通过更换线路解决
describe PagesController doRSpec.describe PagesController, :type => :controller do describe PagesController do RSpec.describe PagesController, :type => :controller do
在spec文件夹中的_spec.rb文件中。
也为了防止弃用警告使用expect(response).to be_success而不是response should be_success
PS:没有必要添加require "rails_helper"

当我忘记将require 'spec_helper'添加到我的spec文件的顶部或--require spec_helper到我的.rspec文件时,出现此错误。

另一种方法是为您的规范指定type: :request 。 例如:

 RSpec.describe "Widget management", :type => :request do it "creates a Widget and redirects to the Widget's page" do get "/widgets/new" expect(response).to render_template(:new) post "/widgets", :widget => {:name => "My Widget"} expect(response).to redirect_to(assigns(:widget)) follow_redirect! expect(response).to render_template(:show) expect(response.body).to include("Widget was successfully created.") end end 

取自这里的示例https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec

我加了这个问题

 gem 'rspec' 

到rails项目中我的Gemfile。 它应该是

 gem 'rspec' gem 'rspec-rails' 

(或者只是rspec-rails)。 后

 bundle install 

重新创buildspec目录

 rspec --init 

并把你的xxx_spec.rb文件放在相应的目录中(如果它在spec目录下,将不起作用)。 初学者错误,但也许这有助于某人;)这是帮助我的链接:

https://www.relishapp.com/rspec/rspec-rails/docs/gettingstarted