如何在 RSpec 视图示例组中测试 link_to_unless_current
我正在使用 Rails 3 学习 RSpec 2,虽然到目前为止进展顺利,但我在视图中测试助手 link_to_unless_current 时遇到了问题。我一直在尝试做的是使用视图规范中的简单断言选择来确定是否在以下部分视图(HAML)中生成链接:
%article.post{ :id => "post-#{post.id}" }
%header.post-title
%h2= link_to_unless_current post.title, post
.post-content= raw post.body
但是,我不知道如何让视图规范识别什么“当前”意味着因为它是一个视图规范,它只测试视图,而不测试请求。我知道这在控制器规范中会简单得多,但我认为我应该测试视图在其规范中的功能,并且将此测试移至控制器规范会使事情变得很混乱。我要问的是:有没有办法告诉视图规范,也许在“之前”块中,当前页面是什么?另外,我在组织测试方面做得正确吗?该测试是否应该正确地驻留在控制器规范中?
I'm learning RSpec 2 with Rails 3 and while it's been going along quite nicely so far, I'm having a problem testing the helper link_to_unless_current in a view. What I've been trying to do is use a simple assert_select from a view spec to determine if a link is being generated in the following partial view (HAML):
%article.post{ :id => "post-#{post.id}" }
%header.post-title
%h2= link_to_unless_current post.title, post
.post-content= raw post.body
However, I don't know how to get the view spec to recognize what "current" means because it's a view spec and it only tests the view, not the request. I know this would be a lot simpler in a controller spec, but I think that I should be testing what a view does in its spec and that moving this test out to a controller spec would be confusing things a lot. What I'm asking is: is there any way to tell the view spec, perhaps in a "before" block, what the current page is? Also, am I doing the right thing in respect to organizing my tests? Should this test rightfully reside in a controller spec?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,我最终想通了。您必须存根 UrlHelper.current_page 吗?如果传递的 url 选项与您想要充当当前页面的页面匹配,则返回 true:
我仍然不知道这是否是我应该进行 RSpec 测试的方式,但是,无论如何,这有效。 :P
Never mind, I eventually figured it out. You have to stub UrlHelper.current_page? and have it return true if the url options passed match the page you want to act as the current page:
I still don't know if this is the way I should be doing RSpec tests, but, whatever, this works. :P