Rspec 部分问题
我正在编写 View 的规范,它有一个在布局中呈现的菜单(部分)。我想为选定的菜单编写规格。这是代码
it "should have User Administration link" do
template.stub!(:render).and_return(:partial => "layouts/admin/menu")
do_render
#render :partial => "layouts/admin/menu" #do
response.should have_tag('div.menu1')
response.should have_tag('ul') do
response.should have_tag('li')do
with_tag("a[href=?]", admin_users_path)
end
end
end
此规范失败。我没有找到正确的方法来编写部分规范,我尝试过: template.should_receive(:render).with(:partial => "/layout/admin/menu")
也是如此。
谢谢和问候, 普拉文。
I am writing specs for View, that has a menu (which is in a partial) rendered in layout. I want to write specs for selected menu. Here is the code
it "should have User Administration link" do
template.stub!(:render).and_return(:partial => "layouts/admin/menu")
do_render
#render :partial => "layouts/admin/menu" #do
response.should have_tag('div.menu1')
response.should have_tag('ul') do
response.should have_tag('li')do
with_tag("a[href=?]", admin_users_path)
end
end
end
This spec is failing. I am not getting right way to write specs for partials, I have tried with:template.should_receive(:render).with(:partial => "/layout/admin/menu")
too.
Thanks and regards,
Pravin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为这条线:
正在做你认为它正在做的事情。 RSpec 不会调用 template.render :partial => “布局/管理/菜单”。它只会取消渲染并返回 {:partial =>;调用渲染时的“layouts/admin/menu”}。
为什么要进行存根渲染?您希望视图实际呈现,以便响应正文具有您想要测试的数据,除非我遗漏了某些内容(可能是这样)。 :)
I don't think this line:
is doing what you think it's doing. RSpec is not going to call template.render :partial => "layouts/admin/menu". It's just going to stub out render and return {:partial => "layouts/admin/menu"} when render is called.
Why stub render at all? You want the view to actually render so the response body has that data you want to test, unless I'm missing something, which I may be. : )
我意识到这是一个古老的讨论,但我在遇到类似问题时遇到了它,所以也许我的解决方案会帮助其他人。
我的问题是我的模拟方法没有被调用,因为我没有正确理解“with”。这是我所拥有的,但不起作用:
当然,现在很明显为什么它不起作用,我正在调用:
但我的规范正在嘲笑:
所以很自然,我的模拟方法不会被调用。一旦我意识到这一点,我就开始尝试模拟 form_for 以及其他各种东西,但解决方案要容易得多:
hash_include() 正是所缺少的。
所以也许你的问题是相似的:你并没有真正嘲笑你认为你正在嘲笑的东西。
I realize this is an old discussion, but I came across it while having the a similar problem, so maybe my solution will help someone else.
My problem was I was my mocked method wasn't being called because I didn't get the "with" right. Here's what I had, which wasn't working:
Of course it's obvious now why it wasn't working, I was calling:
But my spec was mocking:
So naturally, my mocked method wouldn't be called. Once I realized that, I started trying to mock form_for, and all kinds of other things, but the solution was much easier:
hash_including() was what was missing.
So maybe your problem is similar: you're not really mocking what you think you're mocking.