简体   繁体   English

Rspec rails:如何测试一次控制器操作是否正确地使用修改后的参数重定向到其自身?

[英]Rspec rails: How do I test if a controller action redirects correctly to itself with modified params once?

I have a controller action which (based on the query params) might redirect to itself with some modified params 我有一个控制器操作(基于查询参数)可能会通过一些修改后的参数重定向到自身

class SearchController < ApplicationController
  def search
    if params[:deprecated_param].present?
      params[:new_param] = params.delete(:deprecated_param)
      redirect_to search_path(params), status: :moved_permanently and return
    end
  end
end

Now I would like to make two tests something like this: 现在,我想做两个这样的测试:

require 'spec_helper'

describe SearchController, type: :controller do
  describe 'GET #search' do
    render_views
    context 'a deprecated param is used' do
      it 'should do a redirect' do
        get :search, deprecated_param: 'yes'
        expect(response).to redirect_to("http://mydomain.test/search?new_param=yes") # .once ?
        # expect(final response after redirect).to have_http_status(200)
      end
    end
    context 'when no deprecated param is used' do
      it 'should not do any redirects' do
        get :search, new_param: 'yes'
        expect(response).not_to #... redirect_anywhere?
        expect(response).to have_http_status(200)
      end
    end
  end
end

However they don't work. 但是它们不起作用。 There are two problems: 有两个问题:

  1. In the first test: I can't seem to test the intermediate redirection before hitting the same action again after the redirect. 在第一个测试中:在重定向后再次击中相同动作之前,我似乎无法测试中间重定向。
  2. In the second test: I can't find a way to test that no redirect happened AT ALL. 在第二个测试中:我找不到一种方法来测试所有重定向都没有发生。

I found this related question, but it does not solve it. 我找到了这个相关的问题,但是并不能解决。 RSpec testing redirect to URL with GET params RSpec测试使用GET参数重定向到URL

For the second one I guess you can test the following things: 对于第二个,我想您可以测试以下内容:

expect(response).to render_template(:your_template)

Just add the expected template, if you want no redirection just add the template where you expect to stay. 只需添加期望的模板,如果您不希望重定向,只需将模板添加到您希望保留的位置即可。

For the first one, I am not sure to really understand what you need but I try to advise you the following code: 对于第一个,我不确定是否真的了解您的需求,但是我尝试为您提供以下代码:

describe SearchController, type: :request do
# ...
it 'should do a redirect' do
  get "/search", deprecated_param: 'yes'
  expect(response).to redirect_to("http://mydomain.test/search?new_param=yes")
  follow_redirect!
  expect(response).to have_http_status(200)
  expect(response).to render_template(:your_template) #just to be sur you are on the expected page
end

follow_redirect! follow a single redirect response. 遵循单个重定向响应。 If the last response was not a redirect, an exception will be raised. 如果最后一个响应不是重定向,则会引发异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 3.1 Rspec Rails 2-如何测试此控制器操作? - Rails 3.1 Rspec Rails 2 - How can I test this controller action? 如何使用curl测试我的Rails控制器创建动作..还是可以使用rspec? - how do I use curl to test my Rails controller create action.. or can I use rspec? 如何在带有rspec的Rails控制器中测试关联? - How do I test association in rails controller with rspec? 如何测试params传递到rails 3中的控制器,使用rspec? - how to test params passed into a controller in rails 3, using rspec? 我将如何测试在RSpec控制器测试中修改的参数? - How would I test a param is getting modified in a RSpec controller test? 如何在Rails控制器动作的RSpec测试中指定查询字符串? - How to specifiy the query string in an RSpec test of a Rails controller action? 如何使用rspec在我的控制器的新操作中测试实例变量的赋值? - How do I test the assignment of an instance variable in the new action of my controller with rspec? RSpec / Rails - 如何测试ApplicationController中的方法是否被调用(当我测试子类控制器时)? - RSpec / Rails - How do I test that a method in ApplicationController is called (when I'm testing a subclassed controller)? 我将如何将此 curl 请求转换为此控制器操作的 RSpec 测试 - How would I turn this curl request into an RSpec test for this controller action 如何在控制器导轨外部使用Rails参数进行测试 - How can I test using rails params outside controller rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM