【问题标题】:How can I access helper methods in my decorator spec files using Draper 0.14.0如何使用 Draper 0.14.0 访问装饰器规范文件中的辅助方法
【发布时间】:2012-06-23 03:08:32
【问题描述】:

目前在我的 spec/decorators/product_decorator_spec.rb 中,我有以下内容:

require 'spec_helper'

describe ProductDecorator do
  let(:product) { FactoryGirl.create(:product) }

  subject do
    ProductDecorator.first
  end

  before do
    product
  end

  it 'should render the name attribute with a link to the product page' do
    subject.name.should == h.link_to(product.name, 'test')
  end
end

当我运行我的规范时,我得到以下信息:

F.....

Failures:

  1) ProductDecorator should render the name attribute with a link to the product page
     Failure/Error: subject.name.should == h.link_to(product.name, 'resr')
      NameError:
       undefined local variable or method `h' for #<RSpec::Core::ExampleGroup::Nested_2:0x007fbbf212c8b0>
     # ./spec/decorators/product_decorator_spec.rb:15:in `block (2 levels) in <top (required)>'

Finished in 0.98531 seconds
6 examples, 1 failure

Failed examples:

rspec ./spec/decorators/product_decorator_spec.rb:14 # ProductDecorator should render the name attribute with a link to the product page

根据文档,放置在装饰器文件夹中的规范应该可以访问辅助方法,但是我的规范没有。我也尝试过手动标记我的规格,但似乎没有任何效果。

感谢收看。

【问题讨论】:

  • 别忘了将提供的答案标记为正确

标签: ruby-on-rails ruby-on-rails-3 draper


【解决方案1】:

如果您想访问帮助程序,可以通过your_decorator.h.link_to 进行。

当你设置主题时,你需要确保你调用的东西会被路由到助手,你的 rspec 示例中没有注入任何东西!

在您的示例中,调用辅助方法将是 subject.h.link_to

我也认为你的规范中有很多有线的东西。你对letsubjectbefore 的使用让我有点不安……

这里有一篇关于如何编写干净的 rspec 的精彩文章:http://eggsonbread.com/2010/03/28/my-rspec-best-practices-and-tips/

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,在装饰器的辅助代理 (.h) 上调用辅助方法在测试中不起作用(在 Draper 1.3 中)。我最终解决了这个问题,尽管我对此不太满意:

    my_decorated_object.h.extend ApplicationHelper
    

    您的里程可能会有所不同,具体取决于您在助手中访问的控制器功能的数量。

    【讨论】:

      猜你喜欢
      • 2021-05-20
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多