我开始了一个新的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中,如我发布 –