与 Minitest 集成

要将 `rspec-mocks` 与 minitest 结合使用,只需 `require 'rspec/mocks/minitest_integration'`。

将 `rspec-mocks` 与 Minitest::Test 结合使用

给定一个名为 “test/rspec_mocks_test.rb” 的文件,其中包含

require 'minitest/autorun'
require 'rspec/mocks/minitest_integration'

class RSpecMocksTest < Minitest::Test
  def test_passing_positive_expectation
    dbl = double
    expect(dbl).to receive(:message)
    dbl.message
  end

  def test_failing_positive_expectation
    dbl = double
    expect(dbl).to receive(:message)
  end

  def test_passing_negative_expectation
    dbl = double
    expect(dbl).to_not receive(:message)
  end

  def test_failing_negative_expectation
    dbl = double
    expect(dbl).to_not receive(:message)
    dbl.message
  end

  def test_passing_positive_spy_expectation
    bond = spy
    bond.james
    expect(bond).to have_received(:james)
  end

  def test_failing_positive_spy_expectation
    bond = spy
    expect(bond).to have_received(:james)
  end

  def test_passing_negative_spy_expectation
    bond = spy
    expect(bond).not_to have_received(:james)
  end

  def test_failing_negative_spy_expectation
    bond = spy
    bond.james
    expect(bond).not_to have_received(:james)
  end
end

我运行 `ruby test/rspec_mocks_test.rb --seed 0`

那么它应该失败并输出以下内容

1) 失败
RSpecMocksTest#test_failing_positive_expectation
(Double (anonymous)).message(*(any args))
预期:1 次,使用任何参数
接收:0 次,使用任何参数
2) 失败
RSpecMocksTest#test_failing_negative_expectation
(Double (anonymous)).message(no args)
预期:0 次,使用任何参数
接收:1 次
3) 失败
RSpecMocksTest#test_failing_positive_spy_expectation
(Double (anonymous)).james(*(any args))
预期:1 次,使用任何参数
接收:0 次,使用任何参数
4) 失败
RSpecMocksTest#test_failing_negative_spy_expectation
(Double (anonymous)).james(no args)
预期:0 次,使用任何参数
接收:1 次
8 次运行,0 个断言,4 个失败,0 个错误,0 个跳过

将 `rspec-mocks` 与 Minitest::Spec 结合使用

给定一个名为 “spec/rspec_mocks_spec.rb” 的文件,其中包含

require 'minitest/autorun'
require 'minitest/spec'
require 'rspec/mocks/minitest_integration'

describe "Minitest Spec integration" do
  it 'passes a positive expectation' do
    dbl = double
    expect(dbl).to receive(:message)
    dbl.message
  end

  it 'fails a positive expectation' do
    dbl = double
    expect(dbl).to receive(:message)
  end

  it 'passes a negative expectation (using to_not)' do
    dbl = double
    expect(dbl).to_not receive(:message)
  end

  it 'fails a negative expectation (using not_to)' do
    dbl = double
    expect(dbl).not_to receive(:message)
    dbl.message
  end
end

我运行 `ruby spec/rspec_mocks_spec.rb --seed 0`

那么它应该失败并输出以下内容

1) 失败
Minitest Spec 集成#test_0002 失败了一个正向预期
(Double (anonymous)).message(*(any args))
预期:1 次,使用任何参数
接收:0 次,使用任何参数
2) 失败
Minitest Spec 集成#test_0004 失败了一个负向预期(使用 not_to)
(Double (anonymous)).message(no args)
预期:0 次,使用任何参数
接收:1 次
4 次运行,4 个断言,2 个失败,0 个错误,0 个跳过

在 Minitest::Spec 中,加载 `rspec-mocks` 之前加载 `rspec-expectations`

给定一个名为 “spec/rspec_mocks_spec.rb” 的文件,其中包含

require 'minitest/autorun'
require 'minitest/spec'
require 'rspec/mocks/minitest_integration'
require 'rspec/expectations/minitest_integration'

describe "Minitest Spec integration" do
  it 'passes a positive expectation' do
    dbl = double
    expect(dbl).to receive(:message)
    dbl.message
  end

  it 'fails a positive expectation' do
    dbl = double
    expect(dbl).to receive(:message)
  end

  it 'passes a negative expectation (using to_not)' do
    dbl = double
    expect(dbl).to_not receive(:message)
  end

  it 'fails a negative expectation (using not_to)' do
    dbl = double
    expect(dbl).not_to receive(:message)
    dbl.message
  end

  it 'can use both minitest and rspec expectations' do
    expect(1 + 3).must_equal 4
    expect(1 + 3).to eq 4
  end
end

我运行 `ruby spec/rspec_mocks_spec.rb --seed 0`

那么它应该失败并输出以下内容

1) 失败
Minitest Spec 集成#test_0002 失败了一个正向预期
(Double (anonymous)).message(*(any args))
预期:1 次,使用任何参数
接收:0 次,使用任何参数
2) 失败
Minitest Spec 集成#test_0004 失败了一个负向预期(使用 not_to)
(Double (anonymous)).message(no args)
预期:0 次,使用任何参数
接收:1 次
5 次运行,6 个断言,2 个失败,0 个错误,0 个跳过

在 Minitest::Spec 中,加载 `rspec-expectations` 之后加载 `rspec-mocks`

给定一个名为 “spec/rspec_mocks_spec.rb” 的文件,其中包含

require 'minitest/autorun'
require 'minitest/spec'
require 'rspec/expectations/minitest_integration'
require 'rspec/mocks/minitest_integration'

describe "Minitest Spec integration" do
  it 'passes a positive expectation' do
    dbl = double
    expect(dbl).to receive(:message)
    dbl.message
  end

  it 'fails a positive expectation' do
    dbl = double
    expect(dbl).to receive(:message)
  end

  it 'passes a negative expectation (using to_not)' do
    dbl = double
    expect(dbl).to_not receive(:message)
  end

  it 'fails a negative expectation (using not_to)' do
    dbl = double
    expect(dbl).not_to receive(:message)
    dbl.message
  end

  it 'can use both minitest and rspec expectations' do
    expect(1 + 3).must_equal 4
    expect(1 + 3).to eq 4
  end
end

我运行 `ruby spec/rspec_mocks_spec.rb --seed 0`

那么它应该失败并输出以下内容

1) 失败
Minitest Spec 集成#test_0002 失败了一个正向预期
(Double (anonymous)).message(*(any args))
预期:1 次,使用任何参数
接收:0 次,使用任何参数
2) 失败
Minitest Spec 集成#test_0004 失败了一个负向预期(使用 not_to)
(Double (anonymous)).message(no args)
预期:0 次,使用任何参数
接收:1 次
5 次运行,6 个断言,2 个失败,0 个错误,0 个跳过