nearprotocol - Changing VMContext attributes during tests
问题描述
I want to write tests that require the ability to change predecessor accounts mid-test. But I couldn't find a way to change the VMContext dynamically.
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
}
解决方案
When you call ‘testing_env!‘ again with a new context within the same test, it will keep the old storage, but use the new context.
Take a look at tests for fungible token example https://github.com/nearprotocol/near-bindgen/blob/75a62c7c1fd46feda614c4e7776d02eeea054ef8/examples/fun-token/src/lib.rs#L395
推荐阅读
- gremlin - 在 Tinkerpop Graph 中重新运行现有路线
- python - 基于 DatetimeIndex 变量对 Pandas 数据框进行重采样
- javascript - 当前页和上一页的url
- javascript - 在 JSel 中转义方括号
- typescript - 如何正确遍历 TypeScript 中的对象
- c++ - 使用指针在 C++ 中解读单词
- c# - 实体框架避免多对多关系中的重复
- sql - 如何删除 SQL Server 中的子文件夹数据库?
- java - Android Resources.openRawResource() 编码问题
- java - 在 Java 中具有组合的 Setter 和 getter