Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby (RSpec)如何检查是否创建了对象?_Ruby_Unit Testing_Testing_Rspec - Fatal编程技术网

Ruby (RSpec)如何检查是否创建了对象?

Ruby (RSpec)如何检查是否创建了对象?,ruby,unit-testing,testing,rspec,Ruby,Unit Testing,Testing,Rspec,我想通过RSpec测试以下Ruby代码中是否发生了预期的异常处理。通过测试,我意识到我不能使用raise\u errormatcher来测试在拯救异常后是否引发了异常 因此,现在我想测试是否创建了CustomError和standarderor的对象,以查看是否按预期引发了错误 test.rb 模块测试模块 课堂测试 类CustomError=10&&arg2 e,则引发CustomError,“内部CustomError” 放置“正在抢救客户错误” 把e 营救标准错误=>e 放置“拯救标准错误

我想通过RSpec测试以下Ruby代码中是否发生了预期的异常处理。通过测试,我意识到我不能使用
raise\u error
matcher来测试在拯救异常后是否引发了异常

因此,现在我想测试是否创建了
CustomError
standarderor
的对象,以查看是否按预期引发了错误

test.rb

模块测试模块
课堂测试
类CustomError=10&&arg2 e,则引发CustomError,“内部CustomError”
放置“正在抢救客户错误”
把e
营救标准错误=>e
放置“拯救标准错误”
把e
确保
放置“arg1:#{arg1},arg2:#{arg2}\n”
结束
结束
结束
测试规格rb

require./test'
模块测试模块
描述测试做什么
描述“func”do

当arg1>=10和arg2时,它会引发CustomError。您试图做的是测试实现细节(方法内部发生的情况),通常这是一个坏主意。如果特定路径导致异常,但您希望该异常被吞没,请测试该异常

考虑


分区(x,y)
x/y
错误=>e
返回“轰!”
结束
无需测试是否已创建了
ZeroDivisionError
,这是一个实现细节(类似于测试私有方法)。测试从外部“可见”的行为

expect(奇怪的分区(1/0))。返回“Boom!”
因为您可能会更改实现:

def_分割(x,y)
如果y==0,则返回“Boom!”
x/y
结束

您的测试将开始失败,即使方法的行为相同。

您试图做的是测试实现细节(方法内部发生的情况),通常这是一个坏主意。如果特定路径导致异常,但您希望该异常被吞没,请测试该异常

考虑


分区(x,y)
x/y
错误=>e
返回“轰!”
结束
无需测试是否已创建了
ZeroDivisionError
,这是一个实现细节(类似于测试私有方法)。测试从外部“可见”的行为

expect(奇怪的分区(1/0))。返回“Boom!”
因为您可能会更改实现:

def_分割(x,y)
如果y==0,则返回“Boom!”
x/y
结束

您的测试将开始失败,即使该方法的行为相同。

我认为有两种可能性

  • 如果要继续断言,可以使用
    have_receive
    而不是
    receive
    。这将允许您在采取行动后保留您的断言

    expect(已描述的类::CustomError)。接收(:new)

  • 正如评论中提到的,测试你的看跌期权。这是检查方法是否符合您的要求的最终方式


  • 我认为有两种可能性

  • 如果要继续断言,可以使用
    have_receive
    而不是
    receive
    。这将允许您在采取行动后保留您的断言

    expect(已描述的类::CustomError)。接收(:new)

  • 正如评论中提到的,测试你的看跌期权。这是检查方法是否符合您的要求的最终方式


  • 为什么不测试输出(
    put
    )是否符合预期?对我来说,感觉异常是一个实现细节,我会专注于预期的输出。这很有意义。谢谢移动期望值
    expect(descripted\u class::CustomError)。要在调用被测单元之前接收(:new)
    descripted\u class.func(11,-11)
    为什么不测试输出(
    put
    )是否符合预期?对我来说,感觉异常是一个实现细节,我会专注于预期的输出。这很有意义。谢谢移动期望值
    expect(descripted\u class::CustomError)。在调用被测单元之前接收(:new)
    ,然后调用被测单元
    descripted\u class.func(11,-11)
    module TestModule
      class Test
        class CustomError < StandardError
        end
    
        def self.func(arg1, arg2)
          raise CustomError, 'Inside CustomError' if arg1 >= 10 && arg2 <= -10
          raise StandardError, 'Inside StandardError' if arg1.zero? && arg2.zero?
        rescue CustomError => e
          puts 'Rescuing CustomError'
          puts e.exception
        rescue StandardError => e
          puts 'Rescuing StandardError'
          puts e.exception
        ensure
          puts "arg1: #{arg1}, arg2: #{arg2}\n"
        end
      end
    end
    
    require './test'
    
    module TestModule
      describe Test do
        describe '#func' do
          it 'raises CustomError when arg1 >= 10 and arg2 <= -10' do
            described_class.func(11, -11)
            expect(described_class::CustomError).to receive(:new)
          end
        end
      end
    end
    
     Failures:
    
      1) TestModule::Test#func raises CustomError when arg1 >= 10 and arg2 <= -10
         Failure/Error: expect(described_class::CustomError).to receive(:new)
         
           (TestModule::Test::CustomError (class)).new(*(any args))
               expected: 1 time with any arguments
               received: 0 times with any arguments
         # ./test_spec.rb:8:in `block (3 levels) in <module:TestModule>'
    
        it 'puts the output' do
            expect do
               described_class.func(11, -11)
            end.to output('Some string you want to print').to_stdout
        end