Rails 测试单元 - 带有 content_for 的助手
module ApplicationHelper
def title(page_title, show_title = true)
content_for(:title) do
page_title.to_s
end
@show_title = show_title
end
end
有人知道如何使用测试单元测试这个助手吗?
module ApplicationHelper
def title(page_title, show_title = true)
content_for(:title) do
page_title.to_s
end
@show_title = show_title
end
end
Anyone knows how can I test this helper using test unit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于 Rails 中的任何帮助程序测试,您始终从测试/单元/帮助程序开始。
由于这是一个 ApplicationHelper,请使用名为
application_helper_test.rb
的文件在该文件中,您可以拥有类似的内容
您可以通过照常调用方法来测试助手中返回的任何内容,并断言已发送某些内容后退。
不知道你在做什么,就我个人而言,这种方法发生了太多事情,但这可能只是我。
我会把这两个分开,这样你的助手只返回一个 page_title,另一个则返回一个“show_title”,无论是什么。或者就像你切换说“我应该在页面上显示这个标题”?
For any helper testing in rails, you always start in tests/unit/helpers.
Since this is a ApplicationHelper, use the file called
application_helper_test.rb
In that file you can have something like
You can test whatever is returned in a helper by just calling the method as usual, and asserting something is sent back.
Not knowing what you are doing, personally, there is too much going on in this method, but that could just be me.
I'd break these two out, so that your helper is just returning a page_title and another one is returning a "show_title" whatever that is. or is that like your switch to say " I should show this title on a page"?