小编Chr*_*yer的帖子

如何使用"link_to"和"current_page?"测试RSpec a Rails插件

我正在编写一个Rails插件,可以在视图中构建菜单.我正在使用link_to构建链接并在当前页面上current_page?设置class="active".

includeð ActionView::Helpers::UrlHelper这样我就可以使用link_to.

为了current_page?在视图中工作,我必须继承当前的类(显然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
Run Code Online (Sandbox Code Playgroud)

我用RSpec得到了这个错误:未定义的方法`link_to'用于#<Spec :: Rails :: Example :: RailsExampleGroup :: Subclass_1:0x24afa90>

任何线索?

plugins rspec ruby-on-rails

4
推荐指数
1
解决办法
3911
查看次数

Ruby访问器有没有办法返回除set变量之外的东西?

我想在编写器访问器中进行一些检查.我的第一个想法是返回一个布尔值.

class MyClass
  def var=(var)
    @var = var
    # some checking
    return true
  end
end

m = MyClass.new
retval = (m.var = 'foo')

=> "foo"
Run Code Online (Sandbox Code Playgroud)

我可以在编写器访问器中设置返回值吗?如果是,我怎样才能得到这个值?

ruby class accessor

4
推荐指数
1
解决办法
866
查看次数

标签 统计

accessor ×1

class ×1

plugins ×1

rspec ×1

ruby ×1

ruby-on-rails ×1