【问题标题】:Where to write test case for this file in initializers in rails?在rails的初始化程序中在哪里为这个文件编写测试用例?
【发布时间】:2012-02-28 10:54:02
【问题描述】:

我在 rails 2.x 应用程序的 intializer 文件夹中创建了一个 common.rb 文件。代码如下

def handle_exception &block
  begin
    yield block
  rescue Exception => ex
    logger.error(ex)
    HoptoadNotifier.notify(ex)
  end
end

对于以上我想写测试用例。我想它应该在功能测试中。如果是这种情况。我不知道我应该如何命名它,意味着我对(控制器名称

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 testing exception-handling functional-testing


    【解决方案1】:

    我假设handle_exception 是一个全局函数,因此您可以使用 RSpec 对其进行测试,如下所示:

    require 'spec_helper'
    
    describe "common" do
    
      it "handles exceptions" do
        logger.should_receive( :error )
        HoptaodNotifier.should_receive( :notify )
    
        handle_exception { raise Exception.new( "This is the error") }
      end
    
    end
    

    【讨论】:

    • 嘿@Wolfgang,感谢您的回复。我知道要编写我已经做过的测试,该测试位于 Rails 默认测试套件中。我的问题是哪个地方适合做这个测试,为什么?
    猜你喜欢
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2010-11-09
    • 1970-01-01
    • 2014-03-22
    • 1970-01-01
    相关资源
    最近更新 更多