如何在不发出请求的情况下测试redirect_to

时间:2015-04-27 14:43:50

标签: ruby-on-rails testing rspec controller specifications

我想测试一下来自application_controller redirect_to正确路径的私有方法。这是我试图做的:

  describe 'application is complete' do
    it 'redirects to the mortgage application show page' do
      controller.send(:load_mortgage_application)
      expects(controller).to receive(:redirect_to).with(mortgage_application_path(controller.locale, mortgage_application))
    end
  end

但我得到错误:

  RuntimeError:
ActionController::RackDelegation#status= delegated to @_response.status=, but @_response is nil: ...

如何在没有回复的情况下进行测试?

//这个工作:

    it 'redirects to the mortgage application show page' do
      controller.expects(:redirect_to).with(mortgage_application_path(controller.locale, mortgage_application))
      controller.send(:load_mortgage_application)
    end

1 个答案:

答案 0 :(得分:0)

首先,我假设您需要在控制器规范内,以便您可以使用get:load_mortgage_application。那时你可以这样做:

response.should redirect_to '/scorecard'

或更新的语法:

expect(response).to redirect_to '/scorecard'
相关问题