Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rspec 3。未定义的局部变量或方法“响应';对于#<;RSpec::ExampleGroups::ConfigsAPI::GETApiConfig:0x007f84d93fbb90>;_Ruby On Rails_Ruby_Rspec_Rspec3 - Fatal编程技术网

Ruby on rails Rspec 3。未定义的局部变量或方法“响应';对于#<;RSpec::ExampleGroups::ConfigsAPI::GETApiConfig:0x007f84d93fbb90>;

Ruby on rails Rspec 3。未定义的局部变量或方法“响应';对于#<;RSpec::ExampleGroups::ConfigsAPI::GETApiConfig:0x007f84d93fbb90>;,ruby-on-rails,ruby,rspec,rspec3,Ruby On Rails,Ruby,Rspec,Rspec3,我想用rspec测试API控制器。 下面是一段简单的代码: require 'rails_helper' describe "GET /api/config" do it 'routes GET to /api/config to Api::V1::ConfigsController#show' do get '/api/config.json' expect(response).to be_success end end 但我有一个错误: 1) Configs

我想用rspec测试API控制器。 下面是一段简单的代码:

require 'rails_helper'  
describe "GET /api/config" do

  it 'routes GET to /api/config to Api::V1::ConfigsController#show' do
    get '/api/config.json'
    expect(response).to be_success
  end

end
但我有一个错误:

1) Configs API GET /api/config routes GET to /api/config to Api::V1::ConfigsController#show
     Failure/Error: expect(response).to be_success
     NameError:
       undefined local variable or method `response' for #<RSpec::ExampleGroups::ConfigsAPI::GETApiConfig:0x007f84d93fbb90>
     # ./spec/routing/api/v1/devise/configs_routing_spec.rb:13:in `block (3 levels) in <top (required)>'
1)配置API GET/API/config路由GET到/API/config到API::V1::ConfigsController#show
失败/错误:期望(响应)。获得成功
名称错误:
未定义的局部变量或方法“response”#
#./spec/routing/api/v1/designe/configus_routing_spec.rb:13:in‘block(3层)in’

rspec3
中,您将执行以下操作:

get '/api/config.json'
expect(response.status).to be(200)
您缺少的是描述块中的类型声明,即:

describe "GET /api/config", type: :controller do
end

rspec3
中,您将执行以下操作:

get '/api/config.json'
expect(response.status).to be(200)
您缺少的是描述块中的类型声明,即:

describe "GET /api/config", type: :controller do
end

您的控制器规格是否在specs/controller目录中?如果不是,则Rspec需要您通过使用元数据
:type=>:controller
标记descripe来明确声明它们是控制器规范这是因为
response
变量仅在您的规范设置为控制器规范时可用,即

require 'rails_helper'  
describe "GET /api/config", type: :controller do

  it 'routes GET to /api/config to Api::V1::ConfigsController#show' do
    get '/api/config.json'
    expect(response).to be_success
  end

end

还可以查看一下,您的控制器规格是否在specs/controller目录中?如果不是,则Rspec需要您通过使用元数据
:type=>:controller
标记descripe来明确声明它们是控制器规范这是因为
response
变量仅在您的规范设置为控制器规范时可用,即

require 'rails_helper'  
describe "GET /api/config", type: :controller do

  it 'routes GET to /api/config to Api::V1::ConfigsController#show' do
    get '/api/config.json'
    expect(response).to be_success
  end

end

还可以查看一下

您可以尝试使用此语法

使用
expect(last_response.status).to be(200)
而不是仅response

这对我有用

供参考:
我使用的是rails 5,您可以尝试使用这种语法

使用
expect(last_response.status).to be(200)
而不是仅response

这对我有用

供参考:
我使用的是rails 5,不确定,但您可以尝试将
类型
添加到测试中。例如,
it'routes GET…,:type=>:request do
不确定,但您可以尝试将
type
添加到测试中。例如,
it'routes GET…,:type=>:request do
它的工作对象是谁?调用未定义变量的方法是如何定义的?@Nakilon我不确定我是否理解你在问OP的问题时问的问题问题是
未定义的“响应”
。您仍然引用此变量。附加另一个方法调用如何定义它的
?这个问题有3个月的历史了-我想他在我回答时编辑了几次。最后,我们成功了它从未被编辑过。。(我只是想了解这个Rspec Rails行为。它是谁工作的?调用未定义变量的方法是如何定义的?@Nakilon我不确定我是否理解你在问什么在OP的问题中问题出在
未定义的“response”
。你仍然引用这个变量。向它添加另一个方法调用如何使它
>定义的
?这个问题已经3个月了-我想他在我回答的时候编辑了几次。最后,我们成功了。它从未被编辑过。(我只是想了解Rspec Rails的行为。如何将“response”变量添加到“api”规范(vs controller)中,所以所有的“如何测试api”都需要“可以运行的示例?必须包含一个特定的库?(include Rack::Test::Methods似乎不这样做)。如何将“response”变量添加到“api”规范(vs controller),以便所有“How to Test a api”示例都可以运行?必须包含一个特定的库?(include Rack::Test::Methods似乎不这样做)。