在测试期间更改 VMContext 属性
Posted
技术标签:
【中文标题】在测试期间更改 VMContext 属性【英文标题】:Changing VMContext attributes during tests 【发布时间】:2020-03-16 07:29:42 【问题描述】:我想编写需要能够在测试中更改前任帐户的测试。但我找不到动态更改 VMContext 的方法。
fn get_context(value: u128) -> VMContext
VMContext
current_account_id: "alice.near".to_string(),
signer_account_id: "bob.near".to_string(),
signer_account_pk: vec![0, 1, 2],
predecessor_account_id: "carol.near".to_string(),
input: vec![],
block_index: 0,
account_balance: 0,
is_view: false,
storage_usage: 0,
block_timestamp: 123789,
attached_deposit: value,
prepaid_gas: 10u64.pow(9),
random_seed: vec![0, 1, 2],
output_data_receivers: vec![],
#[test]
fn test_market_creation()
let mut context = get_context(500000000);
let config = Config::default();
testing_env!(context, config);
let mut contract = MyContract::default();
contract.do_something(); // Fire method with "carol.near" as predecessor
// Switch account to "bob.near"
contract.do_something(); // Fire method with "bob.near" as predecessor
【问题讨论】:
【参考方案1】:当您在同一个测试中使用新上下文再次调用“testing_env!”时,它将保留旧存储,但使用新上下文。
查看可替代令牌示例https://github.com/nearprotocol/near-bindgen/blob/75a62c7c1fd46feda614c4e7776d02eeea054ef8/examples/fun-token/src/lib.rs#L395的测试
【讨论】:
【参考方案2】:VMContext 中的字段是公开的。应该可以更改它的任何字段:
#[test]
fn test_market_creation()
let mut context = get_context(500000000);
context.predecessor_account_id = "carol.near".to_string();
let config = Config::default();
testing_env!(context, config);
let mut contract = MyContract::default();
contract.do_something(); // Fire method with "carol.near" as predecessor
// Switch account to "bob.near"
contract.do_something(); // Fire method with "bob.near" as predecessor
【讨论】:
以上是关于在测试期间更改 VMContext 属性的主要内容,如果未能解决你的问题,请参考以下文章
H2 嵌入式数据库在 Spring Boot 测试期间未获取属性
在播放期间更改 AVPlayerItem 的 videoComposition 属性 (AVMutableVideoComposition)