Joã*_*iga 10 testing unit-testing rust rust-macros
运行 rust 单元测试时,使用属性宏 #[should_panic(expect = )] 来断言测试因正确的错误消息而发生恐慌(这意味着它在您希望它发生恐慌的行处发生恐慌,而不是因为不同的错误。
但是,在使用在errors.rs 文件中定义的标准化错误消息的应用程序中,不可能将错误消息的常量作为expect 的参数传递。
这:
pub const ERR_001: &str = "ERR_001 message";
#[test]
#[should_panic(expected = ERR_001)]
fn test_function() {
        //test code here
}
产生以下错误:
error: expected unsuffixed literal or identifier, found `ERR_001`
  --> staking/src/storage.rs:74:31
   |
74 |     #[should_panic(expected = ERR_001)]
   |                    
有没有办法将常量或变量传递给 #[should_panic(expected = )] 宏?或者是否有必要总是写消息而不是?
小智 -2
根据 Rust 版本,panic并不总是格式化字符串。在 Rust 2018 和 2015 版本中:
panic!("problem: {reason}")
按字面意思返回
"problem: {reason}"
可能是恐慌格式不正确?