gpt4 book ai didi

ruby-on-rails - 如何使用 "link_to"和 "current_page?"使用 RSpec 测试 Rails 插件

转载 作者:行者123 更新时间:2023-12-04 05:33:41 27 4
gpt4 key购买 nike

我正在编写一个在 View 中构建菜单的 Rails 插件。我正在使用 link_to建立链接和current_page?设置 class="active"在当前页面。

我已经 include d ActionView::Helpers::UrlHelper所以我可以使用 link_to .

获取current_page?在 View 中工作,我不得不继承当前类(显然是 ActionView::Base )并且它工作得很好。不幸的是,这完全破坏了 RSpec 的测试。

我打电话 link_tocurrent_page?像这样:

def menu(options = {}, &block)
carte = my_menu.new(self)
yield carte
carte.to_s unless carte.empty?
end

class my_menu
include ActionView::Helpers::TagHelper
include ActionView::Helpers::UrlHelper

def initialize(base)
@base = base
end

def link
@base.link_to(name, url_options, html_options)
@base.current_page?(url_options)
end
end

我在 RSpec 中遇到了这个错误:
# 的未定义方法`link_to'

有什么线索吗?

最佳答案

include ActionView::Helpers::UrlHelper

在您的 Spec 中应该可以解决问题。或者在您的 spec_helper.rb 中。我认为这基本上是 View 规范默认所做的。

关于ruby-on-rails - 如何使用 "link_to"和 "current_page?"使用 RSpec 测试 Rails 插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/195334/

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