0

I am using Ruby Unit test in code like below.

assert_equal(x, 'strin')

I want to change a variable if this test fails and test should be still fail. How can I do that? I did the same in python using following code.

try:
  self.assertEqual(self.client.testString(''), '')
except AssertionError, e:
  test_basetypes_fails = True
  raise AssertionError( e.args )
Chamila Wijayarathna
  • 1,815
  • 5
  • 30
  • 54

2 Answers2

2

The #assert_equal method in Ruby's test/unit library raises a Test::Unit::AssertionFailedError or (MiniTest::Assertion in ruby 1.9.3 or later) on failed assertions, so:

begin
  assert_equal(x, 'strin')
rescue Test::Unit::AssertionFailedError
  # whatever code
  raise
end
Paweł Obrok
  • 22,568
  • 8
  • 74
  • 70
1

The assert_equal method in Ruby's unit library doesn't raise an exception so you can't use the same approach you did in python but what you want to do is easy enough. You're really just checking the equality of two values and doing something based on that:

if x == 'strin'
  # do something here when the "assert" succeeds
else
  test_basetypes_fails = true
  assert_equal(x, 'strin') # fail the test as normal
end

The point is to use the == directly to perform your initial equality check without making the test fail.

DiegoSalazar
  • 13,361
  • 2
  • 38
  • 55