Ruby-on-rails Rails/Rspec - 在控制器中测试重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16441280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 21:56:46  来源:igfitidea点击:

Rails/Rspec - testing a redirect in the controller

ruby-on-railsredirectrspecroutes

提问by TheLegend

So I am currently writing a test for a controller in an existing controller that just didn't have one before. What I want to test is a redirect that happens when someone is not allowed to edit something vs someone that is allowed to edit it.

因此,我目前正在为之前没有的现有控制器中的控制器编写测试。我想测试的是当不允许某人编辑某些内容与允许对其进行编辑的某人时发生的重定向。

the controller action being edit

正在编辑的控制器动作

def edit
  if [email protected]? || admin?
    @company = @scorecard.company
    @custom_css_include = "confirmation_page"
  else
    redirect_to :back
  end
end

So if a scorecard has been reviewed then only an admin can edit that score. The routes for that controller..

因此,如果已审核记分卡,则只有管理员才能编辑该分数。该控制器的路由..

# scorecards
resources :scorecards do
  member do
    get 'report'
  end
  resources :inaccuracy_reports, :only => [:new, :create]
end

and finally the test

最后是测试

  require 'spec_helper'

  describe ScorecardsController do

    describe "GET edit" do
      before(:each) do
        @agency = Factory(:agency)
        @va = Factory(:va_user, :agency => @agency)
        @admin = Factory(:admin)
        @company = Factory(:company)
        @scorecard = Factory(:scorecard, :level => 1, :company => @company, :agency => @agency, :reviewed => true)
        request.env["HTTP_REFERER"] = "/scorecard"
      end

      context "as a admin" do
        before(:each) do
          controller.stub(:current_user).and_return @admin
        end

        it "allows you to edit a reviewed scorecard" do
          get 'edit', :id => @scorecard.id
          response.status.should be(200)
        end
      end

      context "as a va_user" do
        before(:each) do
        controller.stub(:current_user).and_return @va
      end

      it "does not allow you to edit a reviewed scorecard" do
        get 'edit', :id => @scorecard.id
        response.should redirect_to :back
      end
    end
  end
end

so a va when trying to edit a reviewed score will be redirected back, where an admin won't.

因此,尝试编辑已审核分数时的 va 将被重定向回,而管理员则不会。

but when running this through rspec I get

但是当通过 rspec 运行它时,我得到

ScorecardsController
  GET edit
    as a admin
      allows you to edit a reviewed scorecard
    as a va_user
      does not allow you to edit a reviewed scorecard (FAILED - 1)

Failures:

  1) ScorecardsController GET edit as a va_user does not allow you to edit a reviewed scorecard
     Failure/Error: response.should redirect_to :back
   Expected response to be a redirect to </scorecard> but was a redirect to <http://test.host/>
     # ./spec/controllers/scorecards_controller_spec.rb:33:in `block (4 levels) in <top (required)>'

Finished in 0.48517 seconds
2 examples, 1 failure

so I don't know if its working or not since I set the request.env["HTTP_REFERER"] = "/scorecard"as the place that should be the :backas it where. or am I missing the idea all together looking at httpstatusthere are the 300 responses that I could use but I wouldn't know where to start?

所以我不知道它是否有效,因为我将它设置request.env["HTTP_REFERER"] = "/scorecard"为应该:back作为它的地方。或者我是否在看httpstatus时错过了这个想法,那里有 300 个我可以使用的回复,但我不知道从哪里开始?

any help would be awesome

任何帮助都是极好的

EDIT

编辑

I could test it by doing it like this

我可以通过这样做来测试它

...
response.status.should be(302)

but I got the idea from this questionand it sounds like this could be powerful as it specifies the url redirected to.

但我从这个问题中得到了这个想法,听起来这可能很强大,因为它指定了重定向到的 url。

Anyone have a working test like this?

有人有这样的工作测试吗?

采纳答案by Billy Chan

This line has problem

这条线有问题

response.should redirect_to :back

The logic is not correct. You should expect #editto redirect to :backpath you set before, which is /scorecard. But you set :backhere. In the context of Rspec, :backshould be empty at each example.

逻辑不正确。您应该期望#edit重定向到:back您之前设置的路径,即/scorecard. 但是你设置:back在这里。在 Rspec 的上下文中,:back在每个示例中都应为空。

To revise, just set it as

要修改,只需将其设置为

response.should redirect_to '/scorecard'

回答by Mugur 'Bud' Chirica

To make the test more readable you can do this: (rspec ~> 3.0)

为了使测试更具可读性,您可以这样做:(rspec ~> 3.0)

expect(response).to redirect_to(action_path)

回答by aldrien.h

For testing if redirects happened, with no matching route

用于测试重定向是否发生,没有匹配的路由

(just to test redirection, i used this when route is too long :D ). You can simply do like:

(只是为了测试重定向,我在路由太长时使用了这个:D)。您可以简单地执行以下操作:

expect(response.status).to eq(302) #redirected