2012-01-23 3 views
0

통합 테스트에서 RSpec에 (내가 하드 코드 값이없는 것을 알고)에서 다음호출 내가 할 노력하고있어

get '/api/get-other-items?id=5109' 

내가 찾을 수있는 가장 가까운이었다 : call a specific url with rspec하지만 단 하나의 항목 만 선택합니다. 내가

undefined local variable or method `get_other_items' for #<RSpec::Core::ExampleGroup::Nested_1:0x007f938fd65590> 

얻을 수 있지만 경로가 존재 내가

get get_other_items, :id => '5109' q 

로 실행하면

get :controller => 'api', :action => 'get-other-items', :id => '5109' 
get 'api/get-other-items', :id => '5109'  

이 나에게 bad argument(expected URI object or URI string)

을주고있다 :

나는 다음과 같은 시도
Mon Jan 23$ rake routes | grep get_other_items 
get_other_items  /api/get-other-items(.:format)  {:controller=>"api", :action=>"get_other_items"} 

어떻게하면 간단하게 수행 할 수 있습니까?

들으

대답 한 의견에 대한 업데이 트 여기에 문제의 RSpec에 코드입니다 :

it "testing getting other items for menu item" do 
    get get_other_items_path(:id => '5109') 
    JSON.parse(response.body) 
    puts response.body 

월 1월 23일 $ RSpec에 요청/get_other_items_spec.rb

F 

Failures: 

    1) GetOtherItems testing getting other items for menu item 
    Failure/Error: JSON.parse(response.body) 
    JSON::ParserError: 
    743: unexpected token at 'this is not found   ' 
    # ./requests/get_other_items_spec.rb:19:in `block (2 levels) in <top (required)>' 

Finished in 13.57 seconds 
1 example, 1 failure 

Failed examples: 

답변

0

호출은 get get_other_items_path(:id => 5109)이어야합니다. 라우터 이름에 path을 추가해야합니다. 상대 경로 대신 전체 URL을 사용하려면 e 또는 url을 입력하십시오.

+0

thx하지만 여전히 오류가 발생하는 것은 내 404가 다시 돌아올 때 라우터에서 선택되지 않는 것 같습니다.'사용'할 라우트를 출력하는 방법이 있습니까 (즉, http 호출을하지 않는 것). – timpone

+0

routes.rb 콘텐츠를 게시 할 수 있습니까? – eugen

+0

thx -이 문제는 분명히 나와 일치합니다 : 'match'/ api/get-other-items '=>'api # get_other_items ', : as => : get_other_items' – timpone

0

가 매개 변수로 :id을 복용처럼 당신이 :id 내가 다음을 참조 기대 보내려면 귀하의 경로, 보이지 않는 :

get_other_items /api/get-other-items/:id(.:format)  {:controller=>"api", :action=>"get_other_items"} 

내가 생각 생성 된 경로의 구조를 감안할 때 match을 사용하여 경로를 정의합니다 (경로에 HTTP 동사가 없음). 이 시도 해결하려면 : 당신이 member 경로보다는 collection 경로를 지정한처럼

match 'api/get-other-items/:id' => 'api#get_other_items' 

을 대신 당신이 편안 경로를 사용하는 경우 것은 다음 보인다. A collection 경로는 :id이 아니며 지정한 유형의 많은 레코드를 반환하도록 설계되었습니다. 그것은 member 경로는 다음 사용하도록하려면 : 일단

resources :api do 
    get 'get_other_items', :on => :member 
end 

을이 당신이 당신의 ApiController 사양에 RSpec에에 다음을 시도 할 수 있어야합니다 작업 :

get :get_other_items, :id => '5109' 

이러한 옵션 중 어느 것도 작업하는 경우 우리가 뭔가 다른 것을 시도 할 수 있도록 당신의 노선 항목을 게시하십시오.

관련 문제