Ruby on rails 在Rails中的应用程序_helper.rb代码中测试flash消息方法

Ruby on rails 在Rails中的应用程序_helper.rb代码中测试flash消息方法,ruby-on-rails,ruby-on-rails-3,testing,Ruby On Rails,Ruby On Rails 3,Testing,我对测试驱动开发有点陌生,我想学习如何覆盖尽可能多的代码,这样当我在Rails中制作更复杂的应用程序时,我就能够防止引入bug 我在application\u helper.rb中有一些代码,它将flash消息样式化为Twitter引导类,我想为我编写的代码编写一个测试,因此如果有任何更改,我会在它成为一个小问题之前知道 #application_helper.rb module ApplicationHelper def flash_class(type) case type

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

我在
application\u helper.rb
中有一些代码,它将flash消息样式化为Twitter引导类,我想为我编写的代码编写一个测试,因此如果有任何更改,我会在它成为一个小问题之前知道

#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.html.erb
&时代;
我将编写什么类型的测试来测试
应用程序\u helper.rb
中的代码是否正常工作,我将如何编写该测试?我也在使用gem编写测试,但我不在乎测试是否是用标准的Rails
test\u编写的,带有很多下划线


我正在使用Ruby 1.9.3(补丁级别327)和Rails 3.2.13编写应用程序。我正在开发的应用程序的报告是这样的:

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
class ApplicationHelperTest
在Isolation中测试helper方法我该如何编写它?我编写的唯一测试是表单验证的单元测试:(@apheading虽然这个答案在更一般的范围内是好的,但我真的只想有一个例子,我可以实现到我现有的代码中,这样我就可以更好地理解它的机制。我运行了那个测试,它返回了
test/application\u helper\u test.rb:1:in'':未初始化常量测试(NameError)
我给出了总体思路。我以前从未使用过shoulda上下文,因此我不熟悉如何将其与Rails应用程序集成。您是否有其他测试可以使用?shoulda上下文可以与普通Rails测试结合使用。包含一些我为表单验证编写的测试。好的,它看起来像您的测试s继承自ActiveSupport::TestCase。您在这里也尝试过吗?我尝试过使用
class ApplicationHelperTest打开测试
这就是您得到的结果,返回的错误与以前相同