gpt4 book ai didi

ruby-on-rails - 无方法错误 : undefined method `get' when running rspec and making get/visit calls

转载 作者:数据小太阳 更新时间:2023-10-29 07:06:15 25 4
gpt4 key购买 nike

我完全被困在这里,希望有人能指出我正确的方向。我正在尝试使用 rspec 来测试我的网络路由。我按照这里的例子:

https://www.relishapp.com/rspec/rspec-rails/docs/request-specs/request-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” 没有方法错误: # 的未定义方法“get” # ./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 吗?我想只要将它放在它下面的请求文件夹中就应该假定它们是请求吗?

关于ruby-on-rails - 无方法错误 : undefined method `get' when running rspec and making get/visit calls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11870763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com