2

我对测试驱动开发有点陌生,我想学习如何覆盖尽可能多的代码,这样当我在 Rails 中制作更复杂的应用程序时,我将能够防止引入错误。

我有一些代码,application_helper.rb其中样式会向 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">&times;</button>
    <%= message %>
  </div>
<% end %>

我将编写哪种类型的测试来测试代码是否application_helper.rb有效,我将如何编写该测试?我还在使用shoulda-context gem 进行测试编写,但我不在乎测试是否以标准 Railstest_with_lots_of_underscores样式编写。

我正在使用Cloud9使用 Ruby 1.9.3(补丁级别 327)和 Rails 3.2.13 编写应用程序。我正在开发的应用程序的 repoosiroty在这个 Github 存储库中

4

1 回答 1

0

像这样的东西怎么样:

class ApplicationHelperTest < Test::Unit::TestCase
  context "flash_class" do

    should "map :alert symbol to 'alert-error' string" do
      assert_equal 'alert-error', flash_class(:alert)
    end

    should "map :notice symbol to 'alert-info' string" do
      assert_equal 'alert-info', flash_class(:notice)
    end

    should "map anything else to empty string" do
      assert_equal '', flash_class(:blah)
    end

  end
end
于 2013-04-18T16:22:19.440 回答