簡體   English   中英

當Rails測試顯然存在時無法找到路由?

[英]Rails test can't find route when it clearly exists?

好吧,所以我很累,並且對鐵桿很新,所以可能錯過了一些超級基本的東西。 無論如何,我剛剛開始了一個新項目並且正在為靜態頁面實現一個簡單的控制器。 寫了一些單元測試,以確保我的所有路線都正確(到目前為止只有4個)。 其中三個通過,但第四個給了我這個錯誤信息:

  1) Error:
StaticPagesControllerTest#test_terms_of_service_should_return_success:
ActionController::UrlGenerationError: No route matches {:action=>"terms", :controller=>"static_pages
"}
    test/controllers/static_pages_controller_test.rb:18:in `block in <class:StaticPagesControllerTes
t>'

它說它找不到路線。 但是,當我耙路線時,它顯然存在:

  about GET  /about(.:format)   static_pages#about
contact GET  /contact(.:format) static_pages#contact
privacy GET  /privacy(.:format) static_pages#privacy
  terms GET  /terms(.:format)   static_pages#terms_of_service

這是routes.rb的一部分:

  get 'about' => 'static_pages#about'
  get 'contact' => 'static_pages#contact'
  get 'privacy' => 'static_pages#privacy'
  get 'terms' => 'static_pages#terms_of_service'

這是失敗的測試代碼:

 test "terms_of_service should return success" do
    get 'terms'
    assert_response :success
    assert_select 'title', "Terms Of Service"
 end

我可以直接在瀏覽器中訪問localhost:3000 / terms並且它可以工作。 知道這里發生了什么嗎?

控制器測試不測試路由,測試操作。 你要:

get 'terms_of_service'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM