2016-08-26 7 views
0

이상한 문제가 있습니다. 로컬 레일 앱을 시작하고 http://localhost:3000/static_pages/help으로 이동하면 내가 만든 페이지를 볼 수 있습니다. 그러나 필자가 작성한 테스트 케이스는 다르게 말한다.루비 레일 테스트 케이스가 실패했지만 실제 앱이 작동합니다.

static_pages_controller_test.rb 그것은이 오류와 함께 실패

require 'test_helper' 

class StaticPagesControllerTest < ActionController::TestCase 
    test "should get home" do 
    get :home 
    assert_response :success 
    end 

    test "should get help" do 
    puts static_pages_help_url 
    puts static_pages_help_path 
    get static_pages_help_url 
    assert_response :success 
    end  

end 

, $ 빈의 출력/레이크 시험 : 여기

Running: 

..http://test.host/static_pages/help 
/static_pages/help 
E 

Finished in 0.466745s, 8.5700 runs/s, 4.2850 assertions/s. 

    1) Error. 

StaticPagesControllerTest#test_should_get_help: 
ActionController::UrlGenerationError: No route matches {:action=>"http://test.host/static_pages/help", :controller=>"static_pages"} 
    test/controllers/static_pages_controller_test.rb:12:in `block in <class:StaticPagesControllerTest>' 

routes.rb

Rails.application.routes.draw do 
    get 'static_pages/home' 

    get "static_pages/help" 
end 
입니다

여기에 있습니다. 브라우저에서/static_pages/도움말을 탐색 할 때 나는 또한 그들을 볼 수 있습니다로 static_pages_controller.rb

class StaticPagesController < ApplicationController 
    def home 
    end 

    def help 
    end 
end 

이 두 파일

app/views/static_pages/home.html.erb 
app/views/static_pages/help.html.erb 

가 존재한다. 나는 몇 시간 동안 웹을 검색했다. 단서가 없다.

$ rails --version 
Rails 4.2.7.1 
$ ruby --version 
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux] 

나는 뭔가를 놓쳐 야합니다. 도와주세요.

답변

2

컨트롤러 사양을 작성 중이므로 GET의 매개 변수는 action (컨트롤러 메서드)이어야합니다. 하지만 URL을 전달하고 있습니다. 오류 메시지를 보면 에 "http://test.host/static_pages/help"이 전달 된 것을 알 수 있습니다. 따라서 컨트롤러 메서드의 이름을 URL이 아닌 symbol으로 전달하십시오. 시도해보십시오.

get :help 

help이 컨트롤러 동작임을 유의하십시오.

그러나 integration 테스트를 작성하는 데 관심이 있으시면 ActionController::TestCase이 아닌 ActionDispatch::IntegrationTest에서 상속해야합니다. 그래서 스펙이 이와 같이 보일 것입니다.

class StaticPagesControllerTest < ActionDispatch::IntegrationTest 
    test "should get home" do 
    get static_pages_home_url 
    assert_response :success 
    end 

    test "should get help" do 
    get static_pages_help_url 
    assert_response :success 
    end   
end 

이 도움이 http://weblog.jamisbuck.org/2007/1/30/unit-vs-functional-vs-integration.html

희망을보고, 통합 및 컨트롤러 테스트에 대한 자세한 내용은!

+0

빠른 답변 주셔서 감사합니다. 완전히 나를 괴롭히는 이유는 다음과 같습니다.이 페이지의 저자는 왜 https://www.railstutorial.org/book/static_pages#code-default_controller_test 그의 테스트를 작성합니까? 사실 그것은 작동하지 않는 동안! 그것은 시간이 걸렸습니다 ... – CodeKid

+0

@ 코드 키, 하트를 과소 평가하지 마십시오. 그는 그의 물건을 알고있다. 문제는 Hartl이 제시 한 예제 코드를 보면, 실제로 '통합 테스트'이고, 수행하려는 것은 '컨트롤러 스펙'이다. Hartl의 코드와 당신의 클래스 서명을 보라. 당신의 StaticPagesControllerTest는'ActionController :: TestCase'로부터 상속 받고 그의 구현은'ActionDispatch :: IntegrationTest'에서 상속받습니다. 간단히 말하면, Hartl의 코드는 url을 방문하여 응답이 성공인지 확인하려고 시도합니다. 그러나 컨트롤러 사양은 다릅니다. –

+0

이것은 내가 실제로 알고 싶어하는 것에 대한 대답입니다. 이제는 모두 확실합니다. 정말 고맙습니다. – CodeKid