gpt4 book ai didi

ruby-on-rails - Rspec 3. # 的未定义局部变量或方法 `response'

转载 作者:数据小太阳 更新时间:2023-10-29 08:47:02 24 4
gpt4 key购买 nike

我想用 rspec 测试 API Controller 。所以这是一段简单的代码:

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)>'

最佳答案

RSpec 3 中你会做:

get '/api/config.json'
expect(response.status).to be(200)

您缺少的是描述 block 中的类型声明,即:

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

关于ruby-on-rails - Rspec 3. #<RSpec::ExampleGroups::ConfigsAPI::GETApiConfig:0x007f84d93fbb90> 的未定义局部变量或方法 `response',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24776069/

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