gpt4 book ai didi

ruby-on-rails-3 - 如何在 Rails 中 stub 路径生成方法?

转载 作者:行者123 更新时间:2023-12-01 00:01:29 25 4
gpt4 key购买 nike

我正在编写一个 View 规范,它呈现一个包含该行的 View (在 haml 中):

=link_to new_post_path

但规范失败:
ActionController::RoutingError: No route matches {:action=>"new", :controller=>"post"}

我正在尝试 stub new_post_path方法,因为它对 View 规范实际上并不重要,但没有任何运气。

在我的规范中,我尝试了以下两种变体,但没有任何运气:
           stub!(:new_post_path).and_return("this path isn't important")
controller.stub!(:new_post_path).and_return("this path isn't important")

最佳答案

如果您不在 View 规范中,但发现自己需要 stub 路径助手,则以下语法适用于 Rails 4.2.5;您需要 receive_message_chain而是如此处所述:

https://github.com/rspec/rspec-mocks/issues/1038

以机智:
allow(Rails.application).to receive_message_chain(:routes, :url_helpers, :new_post_path).and_return("this path isn't important")

关于ruby-on-rails-3 - 如何在 Rails 中 stub 路径生成方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13845594/

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