Pithikos Pithikos - 5 months ago 27
Ruby Question

Use "vanilla" assert in Rspec?

How can someone use vanilla

assert
in Rspec?

require 'rspec'

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


The error I get:

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


Notice that I don't want to use
assert_equal
,
eq
,
should
, or other mumbo jumbo.

Answer

You can do this pretty easily:

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

(if you want to be able to run the tests by doing ruby foo.rb then you'll need to require rspec/autorun too). This pulls in all of those assertions. If you really don't want any extra assertions, just define your own assert method that raises an exception when the test should fail.

Conversely you can easily use respects expectation syntax outside of rspec by requiring rspec/expectations - rspec3 is designed to be modular.

Comments