我開始了一個新的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
在這種情況下,它的工作原理。但我將引擎OkmBackend安裝在'/'處。我使用一個子域來訪問它。我不必在/ okm_backend前加我的網址。不是? – patdec
可能是這篇文章會幫助http://stackoverflow.com/questions/36922936/ruby-on-rails-app-testing-with-rspec-and-capybara –
@padrec你可能已經掛載在你的主應用程序中,但對於引擎上的測試,您已將其安裝在/ okm_backend中,如我發佈 –