简体   繁体   English

NoMethodError:运行rspec并进行get / visit调用时未定义的方法`get'

[英]NoMethodError: undefined method `get' when running rspec and making get/visit calls

I am completely stuck here, and hoping someone can point me in the right direction. 我完全被困在这里,并希望有人能指出我正确的方向。 I am trying to use rspec to test my webroutes. 我正在尝试使用rspec来测试我的webroutes。 I followed the example here: 我按照这里的例子:

https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-spec

with my spec file being named:api_tests_spec.rb in the spec/requests folder. 将我的spec文件命名为:spec / requests文件夹中的api_tests_spec.rb。

The file is as follows: 该文件如下:

require 'spec_helper'

describe "APITests" do
  describe "GET /regions"  do
    it "should return a valid response" do
      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
      get "/regions.json"
      response.status.should be(200)
      #print response.body
    end
  end
end

Unfortunately the failure message I am getting is as follows: 不幸的是,我得到的失败信息如下:

1) APITests GET /regions should return a valid response Failure/Error: get "/regions.json" NoMethodError: undefined method `get' for # # ./api_tests_spec.rb:7 1)APITests GET / regions应该返回一个有效的响应失败/错误:获取“/regions.json”NoMethodError:未定义的方法`get'for ##。/ api_tests_spec.rb:7

Does anyone have any insight into why the method can't be found? 有没有人知道为什么找不到该方法? Everything I have read seems to suggest that something isn't getting included properly, but the solution always seems to be to move the file into the requests folder (where mine already is). 我读过的所有内容似乎都暗示某些内容未被正确包含,但解决方案似乎总是将文件移动到请求文件夹(我已经在那里)。 Thanks in advance! 提前致谢!

The spec_helper.rb file looks like this: spec_helper.rb文件如下所示:

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}

RSpec.configure do |config|
  # ## Mock Framework
  #
  # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
  #
  # config.mock_with :mocha
  # config.mock_with :flexmock
  # config.mock_with :rr

  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # If true, the base class of anonymous controllers will be inferred
  # automatically. This will be the default behavior in future versions of
  # rspec-rails.
  config.infer_base_class_for_anonymous_controllers = false
end

Ok, after the first answer I posted seemed to work for a bit, it stopped (Still not sure why?!) 好吧,在我发布的第一个答案似乎工作了一下之后,它就停止了(仍然不确定为什么?!)

However, making this change to the api_tests_spec.rb file fixes it: 但是,对api_tests_spec.rb文件进行此更改会修复它:

require 'spec_helper'

describe "APITests", :type => :request do
  describe "GET /regions"  do
    it "should return a valid response" do
      # Run the generator again with the --webrat flag if you want to use webrat methods/matchers
      get "/regions.json"
      response.status.should be(200)
      #print response.body
    end
  end
end

Does anyone have any idea why the :type => :request is required? 有没有人知道为什么:type =>:request是必需的? I thought just by placing this in the requests folder under it should assume they were requests? 我想只是将它放在它下面的requests文件夹中应该假设它们是请求?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM