jef*_*unt 4 rspec stubbing ruby-on-rails-3
我正在编写一个视图规范,它呈现一个包含该行的视图(以haml 格式):
=link_to new_post_path
Run Code Online (Sandbox Code Playgroud)
但规范失败了:
ActionController::RoutingError: No route matches {:action=>"new", :controller=>"post"}
Run Code Online (Sandbox Code Playgroud)
我正在尝试存根该new_post_path
方法,因为它对视图规范实际上并不重要,但没有任何运气。
在我的规范中,我尝试了以下两种变体,但没有任何运气:
stub!(:new_post_path).and_return("this path isn't important")
controller.stub!(:new_post_path).and_return("this path isn't important")
Run Code Online (Sandbox Code Playgroud)
如果您不在视图规范中,但发现自己需要存根路径助手,则以下语法适用于 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")
该方法new_post_path
来自Rails.application.routes.url_helpers
模块。您需要对该模块上的方法进行存根
Rails.application.routes.url_helpers.stub(:new_post_path).and_return("this path isn't important")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4486 次 |
最近记录: |