在 Rspec 中使用"香草"断言?



有人如何在RSPEC中使用vanilla assert

require 'rspec'
describe MyTest do
  it 'tests that number 1 equals 1' do
    assert 1 == 1
  end
end

我遇到的错误:

undefined method `assert' for
#<RSpec::ExampleGroups::Metadata::LoadFile:0x00000002b232a0>

请注意,我不想使用assert_equaleqshould或其他Mumbo Jumbo。

您可以轻松地执行此操作:

require 'rspec/core'
require 'test/unit'
describe 'MyTest' do
  include Test::Unit::Assertions
  it 'tests that number 1 equals 1' do
    assert 1 == 2
  end
end

(如果您想通过执行ruby foo.rb来运行测试,那么您也需要需要rspec/autorun)。这吸引了所有这些断言。如果您真的不想要任何额外的断言,只需定义您自己的assert方法,该方法在测试失败时会引起异常。

相反,您可以通过需要rspec/expectations -RSPEC3设计为模块化。

,您可以轻松地使用RSPEC的期望语法。

配置rspec以使用minitest

RSpec.configure do |rspec|
  rspec.expect_with :stdlib
end

然后,您可以使用标准库中最大的主张。

...或错误

另外,如果您喜欢具有块的断言,则可以使用错误:

require 'wrong'
RSpec.configure do |rspec|
  rspec.expect_with Wrong
end
describe Set do
  specify "adding using the << operator" do
    set = Set.new
    set << 3 << 4
    assert { set.include?(3) }
  end

受RSPEC.Info上的博客文章的启发。

最新更新