根据当前路径改变行为的助手的单元测试?

发布于 2024-11-28 18:48:19 字数 1707 浏览 1 评论 0原文

我正在尝试在 Rails 中测试以下辅助方法:

  def current_has_class_link(text, path, class_name="selected")
    link_to_unless_current(text, path) do
      link_to(text, path, :class => class_name)
    end
  end

我正在尝试进行一个类似这样的测试:

  describe "current_has_class_link" do
    let(:link_path){ listings_path }
    let(:link_text){ "Listings" }

    it "should render a normal link if not on current path" do
      html = "<a href=\"#{link_path}\">#{link_text}</a>"
      current_has_class_link(link_text, link_path).should == html
    end

    it "should add a class if on the links path" do
      # at this point I need to force current_path to return the same as link_path
      html = "<a href=\"#{link_path}\" class=\"selected\">#{link_text}</a>"
      current_has_class_link(link_text, link_path).should == html
    end
  end

现在显然我可以为此使用集成测试,但这对我来说似乎有点过分了。有没有办法可以存根 current_page? 以便它返回我需要的内容?

我尝试这样做

ActionView::Helpers::UrlHelper.stub(current_page?({controller: 'listings', action: 'index'})).and_return(link_path)

但这给了我一个我不太理解的错误:

Failures:

  1) ApplicationHelper current_has_class_link should add a class if on the links path
     Failure/Error: ActionView::Helpers::UrlHelper.stub(current_page?({controller: 'listings', action: 'index'})).and_return(link_path)
     RuntimeError:
       You cannot use helpers that need to determine the current page unless your view context provides a Request object in a #request method
     # ./spec/helpers/application_helper_spec.rb:38:in `block (3 levels) in <top (required)>'

还有其他方法吗?

I'm trying to test the following helper method in rails:

  def current_has_class_link(text, path, class_name="selected")
    link_to_unless_current(text, path) do
      link_to(text, path, :class => class_name)
    end
  end

I'm trying to make a test which is something like this:

  describe "current_has_class_link" do
    let(:link_path){ listings_path }
    let(:link_text){ "Listings" }

    it "should render a normal link if not on current path" do
      html = "<a href=\"#{link_path}\">#{link_text}</a>"
      current_has_class_link(link_text, link_path).should == html
    end

    it "should add a class if on the links path" do
      # at this point I need to force current_path to return the same as link_path
      html = "<a href=\"#{link_path}\" class=\"selected\">#{link_text}</a>"
      current_has_class_link(link_text, link_path).should == html
    end
  end

Now obviously I could use an integration test for this but that seems like overkil to me. Is there a way that I can stub current_page? so that it returns what I need?

I tried to do

ActionView::Helpers::UrlHelper.stub(current_page?({controller: 'listings', action: 'index'})).and_return(link_path)

But that gives me an error which I don't really understand:

Failures:

  1) ApplicationHelper current_has_class_link should add a class if on the links path
     Failure/Error: ActionView::Helpers::UrlHelper.stub(current_page?({controller: 'listings', action: 'index'})).and_return(link_path)
     RuntimeError:
       You cannot use helpers that need to determine the current page unless your view context provides a Request object in a #request method
     # ./spec/helpers/application_helper_spec.rb:38:in `block (3 levels) in <top (required)>'

Is there another way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小鸟爱天空丶 2024-12-05 18:48:19

我遇到了同样的问题,并在测试级别将其存根。

self.stub!("current_page?").and_return(true)

I had the same issue and stubbed it at the test level instead.

self.stub!("current_page?").and_return(true)
我很坚强 2024-12-05 18:48:19

Test:Unit 中,您可以使用 attr_reader 将请求设置为方法。

class ActiveLinkHelperTest < ActionView::TestCase

  attr_reader :request

  test "should render a normal link if not on current path" do
    html = "<a href=\"#{link_path}\">#{link_text}</a>"
    assert_equal html, current_has_class_link(link_text, link_path)
  end

  test "should add a class if on the links path" do
    # Path can be set.
    #   note: the default is an empty string that will never match)
    request.path = link_path

    html = "<a href=\"#{link_path}\" class=\"selected\">#{link_text}</a>"
    assert_equal html, current_has_class_link(link_text, link_path)
  end

end

Within Test:Unit you can use an attr_reader to set the request as a method.

class ActiveLinkHelperTest < ActionView::TestCase

  attr_reader :request

  test "should render a normal link if not on current path" do
    html = "<a href=\"#{link_path}\">#{link_text}</a>"
    assert_equal html, current_has_class_link(link_text, link_path)
  end

  test "should add a class if on the links path" do
    # Path can be set.
    #   note: the default is an empty string that will never match)
    request.path = link_path

    html = "<a href=\"#{link_path}\" class=\"selected\">#{link_text}</a>"
    assert_equal html, current_has_class_link(link_text, link_path)
  end

end
蓝礼 2024-12-05 18:48:19

尝试使用:

view.stub!(:current_page?).and_return(true)

Try using:

view.stub!(:current_page?).and_return(true)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文