如何从 RSpec 视图规范测试页面的标题?
我正在学习 RSpec 2 和 Rails 3。为了在每个页面的布局中设置标签的内容,我有一个可用于设置标题然后返回它的帮助程序:
def page_title(subtitle=nil)
if @title.nil?
@title = ["Site Name"]
end
unless subtitle.nil?
@title << subtitle
end
@title.reverse.join " - "
end
从两个页面调用该帮助程序布局(返回标题)和各个视图(设置标题)。现在,我想在视图规范中测试标题设置是否正确。由于布局未渲染,我决定从规范中调用 page_title 并检查返回值是否符合我的预期。但是,这不起作用,并且始终只返回“站点名称”。我应该怎么办?
I'm learning RSpec 2 with Rails 3. In order to set the contents of the tag in the layout for each page, I have a helper that can be used to set the title and then return it:
def page_title(subtitle=nil)
if @title.nil?
@title = ["Site Name"]
end
unless subtitle.nil?
@title << subtitle
end
@title.reverse.join " - "
end
The helper is called from both the layout, where it returns the title, and the individual views, where it sets the title. Now, I want to test in the view specs that the title is being set correctly. Since the layout is not rendered, I have decided to call page_title from the spec and check that the return value is what I expect it to be. However, this doesn't work, and always just returns "Site Name". What should I do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要检查视图规范中页面的标题,请尝试:
因为渲染是在控制器外部调用的,所以需要告知它有关布局的信息。
To check the title of a page in a view spec try:
Because render is being called outside of the controller, it needs to be told about the layout.
我不确定这是否是您的意思,但您可以直接测试布局:
编辑
您还可以测试在视图中调用
page_title
方法:或您可以使用
render_views
进行控制器测试:I'm not sure if this is what you meant, but you could test the layout directly:
EDIT
You could also test that the
page_title
method is called in the view:or you could use a controller test with
render_views
: