加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:54:10 所属栏目:百科 来源:网络整理
导读:目前在我的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 'shoul
目前在我的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示例中没有注入任何内容!

在你的例子中,它将是subject.h.link_to,用于调用辅助方法.

我还认为你的规范中有很多有线的东西.你对let,subject和before的使用对我来说是一种令人不安的…

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读