gpt4 book ai didi

ruby-on-rails - 如何使用 Draper 0.14.0 访问装饰器规范文件中的辅助方法

转载 作者:行者123 更新时间:2023-12-04 22:49:24 25 4
gpt4 key购买 nike

目前在我的 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

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

感谢您的关注。

最佳答案

如果你想访问助手,你可以通过 your_decorator.h.link_to .

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

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

我还认为你的规范中有很多有线的东西。您对 let 的使用情况, subjectbefore对我来说有点令人不安...

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

关于ruby-on-rails - 如何使用 Draper 0.14.0 访问装饰器规范文件中的辅助方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11166493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com