program tip

Rails / Rspec-컨트롤러에서 리디렉션 테스트

radiobox 2020. 12. 8. 07:56
반응형

Rails / Rspec-컨트롤러에서 리디렉션 테스트


그래서 저는 현재 이전에 컨트롤러가 없었던 기존 컨트롤러에서 컨트롤러에 대한 테스트를 작성하고 있습니다. 내가 테스트하고 싶은 것은 누군가가 무언가를 편집하도록 허용되지 않았거나 편집이 허용 된 사람이있을 때 발생하는 리디렉션입니다.

편집중인 컨트롤러 액션

def edit
  if !@scorecard.reviewed? || admin?
    @company = @scorecard.company
    @custom_css_include = "confirmation_page"
  else
    redirect_to :back
  end
end

따라서 스코어 카드를 검토 한 경우 관리자 만 해당 점수를 편집 할 수 있습니다. 해당 컨트롤러의 경로 ..

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

그리고 마지막으로 테스트

  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

따라서 검토 된 점수를 편집하려고 할 때 va는 다시 리디렉션되며 관리자는 그렇지 않습니다.

하지만 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 :back as it where. or am I missing the idea all together looking at httpstatus there are the 300 responses that I could use but I wouldn't know where to start?

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 question and it sounds like this could be powerful as it specifies the url redirected to.

Anyone have a working test like this?


This line has problem

response.should redirect_to :back

The logic is not correct. You should expect #edit to redirect to :back path you set before, which is /scorecard. But you set :back here. In the context of Rspec, :back should be empty at each example.

To revise, just set it as

response.should redirect_to '/scorecard'

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

expect(response).to redirect_to(action_path)

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:

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

참고URL : https://stackoverflow.com/questions/16441280/rails-rspec-testing-a-redirect-in-the-controller

반응형