小编Mat*_*dan的帖子

是否可以使用MiniTest测试类方法(例如Foo.bar)?

如何Foo.bar在不测试bar方法行为(已在其他地方测试过)的情况下测试以下示例中调用的内容?

# Code
class Alpha
  def process
    Foo.bar
  end
end
Run Code Online (Sandbox Code Playgroud)

以下是我到目前为止的规范.不幸的是,这种方法抛出了"已经定义的类"警告,因为Foo已经在我的项目的其他地方定义过了.

 # Spec
 let(:alpha) { Alpha.new }
 let(:klass) { MiniTest::Mock.new }

 subject { alpha.process }

 it "calls Foo.bar" do
   klass.expect(:bar, '')     # Define method call expectation
   Foo = klass                # Redefine Foo as a mock object
   subject                    # Run method being tested
   klass.verify               # Confirm method was called
 end
Run Code Online (Sandbox Code Playgroud)

我不希望我的测试依赖于Foo类,因为这是一个外部依赖,我不想测试响应值Foo.bar,因为这可能会随意改变.

ruby minitest

3
推荐指数
1
解决办法
1810
查看次数

标签 统计

minitest ×1

ruby ×1