【发布时间】:2013-04-11 14:08:45
【问题描述】:
我对测试驱动开发有点陌生,我想学习如何覆盖尽可能多的代码,这样当我在 Rails 中制作更复杂的应用程序时,我将能够防止引入错误。
我在application_helper.rb 中有一些代码,用于将 Flash 消息样式化到 Twitter Bootstrap 类,我想为我编写的代码编写一个测试,所以如果有任何变化,我会在它成为一点问题之前知道它。
#application_helper.rb
module ApplicationHelper
def flash_class(type)
case type
when :alert
"alert-error"
when :notice
"alert-info"
else
""
end
end
end
我的application.html.erb 视图具有以下代码,用于使用上面的帮助方法显示 Flash 消息。
#application.html.erb
<% flash.each do |type, message| %>
<div class="alert <%= flash_class type %>">
<button type="button" class="close" data-dismiss="alert">×</button>
<%= message %>
</div>
<% end %>
我将编写什么类型的测试来测试application_helper.rb 中的代码是否有效,我将如何编写该测试?我也在使用 shoulda-context gem 进行测试编写,但我不在乎测试是否使用标准 Rails test_with_lots_of_underscores 样式编写。
我正在使用Cloud9 使用 Ruby 1.9.3(补丁级别 327)和 Rails 3.2.13 编写应用程序。我正在开发的应用程序的 repoosiroty 是 in this Github repository
【问题讨论】:
-
单独测试辅助方法
-
我该怎么写呢?我写过的唯一测试是表单验证的单元测试:(
-
@apneadiving 虽然这个答案在更广泛的范围内很好,但我真的只想有一个可以在现有代码中实现的示例,以便更好地理解它的机制。
标签: ruby-on-rails ruby-on-rails-3 testing