Capybara在引擎中找不到我的路线

时间:2016-07-24 10:47:54

标签: ruby-on-rails rspec capybara ruby-on-rails-plugins

我为自己开了一个新的Rails 5应用程序,看看我如何使用引擎和简单的测试目的来模块化应用程序。 我决定使用Rspec和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 /配置/ routes.rb中

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

命令 rails app:routes 在终端中的结果:

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替换访问“/ humen”,我会收到此错误:

  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

这是非常基本的。 谢谢,如果你能帮助我。

更新

如果我像这样编写我的规范(由Tom Walpole解答):

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

3 个答案:

答案 0 :(得分:2)

在spec / dummy / config / routes.rb中,您将引擎安装在/ okm_backend用于测试目的

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

这意味着用字符串调用visit需要

visit '/okm_backend/humen'

答案 1 :(得分:1)

更好的解决方案是使用SelectedItems,在您的情况下是:

engine_name.some_path

答案 2 :(得分:0)

您是否尝试在rails控制台中执行app.humen_path?您必须使用该字符串作为URL。