2010-11-19 2 views
3

전통적인 RESTful 방식으로 직접 액세스 할 수없는 컨트롤러가 있지만 특정 URL을 통해서만 액세스 할 수 있습니다.직접 액세스 할 수없는 RSpec 컨트롤러 동작 테스트

일반적으로 get 및 post를 내 컨트롤러 사양에서 사용하여 컨트롤러 작업을 호출하는 데 익숙합니다. 특정 URL을 방문하여 컨트롤러를 사용할 수있는 방법이 있습니까?

이 편집 : 여기

Larzworld::Application.routes.draw do 

    match '/auth/:provider/callback' => 'authentications#create' 

    devise_for :users, :controllers => {:registrations => "registrations"} 

    root :to => 'pages#home' 
end 

내 사양입니다 : 여기

내 경로 여기

require 'spec_helper' 

describe AuthenticationsController do 

before(:each) do 
    request.env["omniauth.auth"] = {"provider" => "twitter", "uid" => "12345678"} 
end 

describe 'POST create' do 

    it "should find the Authentication using the uid and provider from omniauth" do 
    Authentication.should_receive(:find_by_provider_and_uid) 
    post 'auth/twitter/callback' 
    end 
end 

end 

에러가 나는 받게된다

Failures: 
    1) AuthenticationsController POST create should find the Authentication using the uid and provider from omniauth 
    Failure/Error: post 'auth/twitter/callback' 
    No route matches {:action=>"auth/twitter/callback", :controller=>"authentications"} 
    # ./spec/controllers/authentications_controller_spec.rb:13 

Finished in 0.04878 seconds 
1 example, 1 failure 

답변

7

컨트롤러 테스트에서는 네 개의 HTTP 동사 (G ET, POST, PUT, DELETE)에 상관없이 컨트롤러가 RESTful인지 여부를 확인합니다. 그래서 당신은 아닌 편안하고 경로 (Rails3)이있는 경우 :

match 'example' => 'story#example' 

이들 두 테스트를 :

require 'spec_helper' 

describe StoryController do 

    describe "GET 'example'" do 
    it "should be successful" do 
     get :example 
     response.should be_success 
    end 
    end 

    describe "POST 'example'" do 
    it "should be successful" do 
     post :example 
     response.should be_success 
    end 
    end 

end 

것 모두 패스 경로가 어떤 동사를 허용하기 때문이다.

편집

난 당신이 컨트롤러 테스트 및 경로 테스트를 혼합하고 생각합니다. 컨트롤러 테스트에서 동작에 대한 논리가 올바르게 작동하는지 확인하려고합니다. 경로 테스트에서 URL이 올바른 컨트롤러/동작으로 가고 params 해시가 올바르게 생성되는지 확인합니다.

그래서 컨트롤러 액션을 테스트하기 위해, 간단하게 수행

post :create, :provider => "twitter"` 

이 경로를 테스트 params_from 또는 route_to (RSpec에 1) (RSpec에 2) 사용하려면 :

describe "routing" do 
    it "routes /auth/:provider/callback" do 
    { :post => "/auth/twitter/callback" }.should route_to(
     :controller => "authentications", 
     :action => "create", 
     :provider => "twitter") 
    end 
end 
+0

좋아, 그건 내가 생각했던 것인데, 그런 다음 내가 편집 한 게시물을보고 내 경로, 테스트 및 오류를 게시합니다. 왜 올바른 동작으로 매핑하지 않는지 이해할 수 없습니다. – TheDelChop

+0

내 편집을 참조하십시오. 여기 라우팅 테스트가 정말로 필요합니다. – zetetic

관련 문제