2016-07-24 92 views
0

我開始了一個新的Rails 5應用程序,以瞭解如何使用引擎和簡單的測試目的模塊化應用程序。 我決定使用Rspec和Capybara進行集成測試。Capybara在引擎中找不到我的路線

我有一個非常基本的功能rspec的:

okinsmen /組件/ okm_backend /規格/功能/ humen_spec.rb

require 'rails_helper' 

feature "Humen process" do 
    scenario "go to index page" do 
    visit '/humen' 
    expect(page).to have_content("List of humen") 
    end 
end 

這是我的路線:

okinsmen /組件/ okm_backend/config/routes.rb

OkmBackend::Engine.routes.draw do 
     root 'dashboards#index' 
     resource :dashboards, only: [:index] 
     resources :humen 
    end 

而命令的結果Rails應用程序:路線在終端:

Prefix Verb URI Pattern Controller#Action 
okm_backend  /okm_backend OkmBackend::Engine 

Routes for OkmBackend::Engine: 
     root GET /      okm_backend/dashboards#index 
    humen GET /humen(.:format)   okm_backend/humen#index 
      POST /humen(.:format)   okm_backend/humen#create new_human GET /humen/new(.:format)  okm_backend/humen#new edit_human GET /humen/:id/edit(.:format) okm_backend/humen#edit 
    human GET /humen/:id(.:format)  okm_backend/humen#show 
      PATCH /humen/:id(.:format)  okm_backend/humen#update 
      PUT /humen/:id(.:format)  okm_backend/humen#update 
      DELETE /humen/:id(.:format)  okm_backend/humen#destroy 

所有看起來不錯,但我總是得到這樣的錯誤:

1) Humen process go to index page 
    Failure/Error: visit '/humen' 

    ActionController::RoutingError: 
     No route matches [GET] "/humen" 

如果我更換訪問「/虎門」的humen_path,我得到這個錯誤:

1) Humen process go to index page 
    Failure/Error: visit humen_path 

    NameError: 
     undefined local variable or method `humen_path' for #<RSpec::ExampleGroups::HumenProcess:0x00000006843ea0> 

爲了解決這個最後一個問題,我要在我的rails_helper.spec加入這一行:

config.include OkmBackend::Engine.routes.url_helpers 

現在測試通過。

但我無法使用字符串URL進行測試。

的應用程序可以在https://github.com/patdec/okinsmen/tree/engines

這是非常基本的來看待。 謝謝,如果你能幫助我。

更新:

如果我寫我的規格是這樣的(由湯姆·沃波爾爲anwered):

scenario "go to index page" do 
    visit '/okm_backend/humen' 
    expect(page).to have_content("Humen list") 
    end 

它的工作原理

但是,這並不:

scenario "display new page" do 
    click_on '/okm_backend/humen/new' 
    expect(page).to have_content("New human") 
    end 

回答

1

在你的spec/dummy/config/routes中。RB你安裝發動機在/ okm_backend用於測試目的

Rails.application.routes.draw do 
    mount OkmBackend::Engine => "/okm_backend" 
end 

這意味着調用visit用繩子將需要

visit '/okm_backend/humen' 
+0

在這種情況下,它的工作原理。但我將引擎OkmBackend安裝在'/'處。我使用一個子域來訪問它。我不必在/ okm_backend前加我的網址。不是? – patdec

+0

可能是這篇文章會幫助http://stackoverflow.com/questions/36922936/ruby-on-rails-app-testing-with-rspec-and-capybara –

+0

@padrec你可能已經掛載在你的主應用程序中,但對於引擎上的測試,您已將其安裝在/ okm_backend中,如我發佈 –

0

你有沒有試過在你的rails中做app.humen_path 安慰?你必須使用該字符串作爲URL。

+0

我得到這個錯誤:NoMethodError:未定義的方法'humen_path」爲# patdec

+0

如果我輸入app.okm_backend.humen_path,我得到'/ humen' – patdec

+0

你做錯了@patdec'app.okm_backend.humen_path'應該只是'okm_backend.humen_path'。如果有人需要,我在下面添加了一個答案。 –

0

一個更好的解決方案是使用engine_name.some_path,這在你的案例:

okm_backend.humen_path 
# returns /okm_backend/humen