簡體   English   中英

NoMethodError:運行rspec並進行get / visit調用時未定義的方法`get'

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

我完全被困在這里,並希望有人能指出我正確的方向。 我正在嘗試使用rspec來測試我的webroutes。 我按照這里的例子:

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

將我的spec文件命名為:spec / requests文件夾中的api_tests_spec.rb。

該文件如下:

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

不幸的是,我得到的失敗信息如下:

1)APITests GET / regions應該返回一個有效的響應失敗/錯誤:獲取“/regions.json”NoMethodError:未定義的方法`get'for ##。/ api_tests_spec.rb:7

有沒有人知道為什么找不到該方法? 我讀過的所有內容似乎都暗示某些內容未被正確包含,但解決方案似乎總是將文件移動到請求文件夾(我已經在那里)。 提前致謝!

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

好吧,在我發布的第一個答案似乎工作了一下之后,它就停止了(仍然不確定為什么?!)

但是,對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

有沒有人知道為什么:type =>:request是必需的? 我想只是將它放在它下面的requests文件夾中應該假設它們是請求?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM