Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.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执行web请求时的存根控制器方法_Ruby On Rails_Ruby_Rspec - Fatal编程技术网

Ruby on rails 使用Rspec执行web请求时的存根控制器方法

Ruby on rails 使用Rspec执行web请求时的存根控制器方法,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,如何在发出请求时存根控制器方法或助手方法 我有一个测试,它会发出一个web请求。该请求发送到控制器,该控制器也调用一个helper方法。在下面的示例中,出于测试目的,我想存根service\u config的返回值,因为service\u config来自AdminsHelper,并对服务执行实际的API请求 我对Rspec和这些类型的测试实践有点陌生,所以在正确测试这个web请求时,我可能也走错了方向 测试: 控制器: class AdminsController < Applicati

如何在发出请求时存根控制器方法或助手方法

我有一个测试,它会发出一个web请求。该请求发送到控制器,该控制器也调用一个helper方法。在下面的示例中,出于测试目的,我想存根
service\u config
的返回值,因为
service\u config
来自
AdminsHelper
,并对服务执行实际的API请求

我对Rspec和这些类型的测试实践有点陌生,所以在正确测试这个web请求时,我可能也走错了方向

测试:

控制器:

class AdminsController < ApplicationController
  include AdminsHelper

  def configs
    response = service_config
    render json: response, status: 200
  end
end
class AdminsController
我希望在这种情况下会出现这种情况

context 'when an invalid parameter is specified' do
  it 'should return a hash of size 0' do
    allow_any_instance_of(AdminsController).to receive(:service_config).and_return({})

    get '/admins/configs/poop'

    json = JSON.parse(response.body)
    expect(json).to eq({})
 end
结束

我希望在这种情况下会发生这种情况

context 'when an invalid parameter is specified' do
  it 'should return a hash of size 0' do
    allow_any_instance_of(AdminsController).to receive(:service_config).and_return({})

    get '/admins/configs/poop'

    json = JSON.parse(response.body)
    expect(json).to eq({})
 end
结束