7

When running rust unit tests it is very useful to utilize the attribute macro #[should_panic(expect = )] to assert that the test is panicking with the correct error message (which means it's panicking at the line you want it to panic and not because of a different error.

However, in applications that use standardized error messages defined in an errors.rs file, it is not possible to pass the constant for the error message as the argument for expect.

This:

pub const ERR_001: &str = "ERR_001 message";

#[test]
#[should_panic(expected = ERR_001)]
fn test_function() {

        //test code here

}

yields the following error:

error: expected unsuffixed literal or identifier, found `ERR_001`
  --> staking/src/storage.rs:74:31
   |
74 |     #[should_panic(expected = ERR_001)]
   |                    

Is there a way to pass a constant or variable to the #[should_panic(expected = )] macro? Or is it necessary to always write message instead of ?

Herohtar
  • 5,347
  • 4
  • 31
  • 41
João A. Veiga
  • 498
  • 3
  • 11

0 Answers0