匿名类的未定义URL助手方法

时间:2017-10-10 15:48:33

标签: ruby-on-rails ruby rspec

我为rails运行了一个脚手架命令,并且无法获得适用于视图的规范。当调用URL帮助程序方法时,它们总是会失败,例如:

  1) engine/foo/bar/index renders a list of foo/bar
 Failure/Error: <td><%= link_to 'Show', foo_bar %></td>

 ActionView::Template::Error:
   undefined method `foo_bar_path' for #<#<Class:0x007fed161bf908>:0x007fed13d25750>

我尝试在规范中包含URL帮助程序,但这给了我另一个错误。

我已经检查了https://github.com/rspec/rspec-rails/issues/476并尝试了在那里发布的解决方案,没有任何帮助。我已经尝试了多个其他解决方案,没有骰子。总而言之,我无法弄清楚为什么或如何解决这个失败。我使用的是ruby版本2.3.0,rails版本4.2.7.1,rspec-core 3.6.0和rails-rspec 3.6.1。

视图的项目目录如下所示:

Project
  app
   views
     engine
       foo
         bars
           index.html.erb
  spec
    views
      engine
        foo
          bars
            index.html.erb_spec.rb

我的index.html.erb文件如下所示:

<p id="notice"><%= notice %></p>

<h1>Listing Foo Bars</h1>

<table>
  <thead>
    <tr>
      <th>Displays</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @foo_bars.each do |foo_bar| %>
      <tr>
        <td><%= foo_bar.display %></td>
        <td><%= link_to 'Show', foo_bar %></td>
        <td><%= link_to 'Edit', edit_foo_bar_path(foo_bar) %></td>
        <td><%= link_to 'Destroy', foo_bar, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Bar', new_foo_bar_path %>

上述文件的规范是:

require 'rails_helper'

RSpec.describe 'engine/foo/bars/index', type: :view do

  before(:each) do
    assign(:foo_bars, [
      Engine::Foo::Bar.create!(display: 'Display'),
      Engine::Foo::Bar.create!(display: 'Display')
    ])
  end

  it 'renders a list of foo/bars' do
    render
    assert_select 'tr>td', text: 'Display'.to_s, :count => 2
  end
end

我的routes.rb是:

Engine::Engine.routes.draw do

  namespace :foo do
    resources :bars
  end
end

感谢任何帮助。

0 个答案:

没有答案