【发布时间】:2015-01-28 22:39:49
【问题描述】:
我有一个 Rails (4.2) 助手,我正在尝试使用 Rspec 3 进行单元测试。
# app/helpers/nav_helper.rb
module NavHelper
def nav_link(body, url, li_class: "", html: {})
li_class += current_page?(url) ? " active" : ""
content_tag(:li, class: li_class) do
link_to(body, url, html)
end
end
end
# spec/helpers/nav_helper_spec.rb
require 'spec_helper'
describe NavHelper do
describe "#nav_link" do
it "creates a correctly formatted link" do
link = nav_link("test", "www.example.com/testing")
...
end
end
end
当我运行测试时,这会引发以下错误:
Failure/Error: link = nav_link("test", "www.example.com/testing")
NoMethodError:
undefined method `content_tag' for #<RSpec::ExampleGroups::NavHelper::NavLink:0x007fe44b98fee0>
# ./app/helpers/nav_helper.rb:5:in `nav_link'
Rails 助手似乎不可用,但我不确定如何包含它们。无论如何,如何测试使用 content_tag 的辅助方法?
更新
添加include ActionView::Helpers::TagHelper 会引发以下错误
uninitialized constant ActionView (NameError)
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 rspec rspec3