Nethereum使用异步方法来获取地址的TransactionCount。
我已经将该方法放入异步任务中:
public async Task<object> GetTxCount(string address)
{
return await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(address).ConfigureAwait(false);
}并试图用来测试它。
[TestMethod]
public async Task TestMethodAsync()
{
string address = "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae";
EthTest.Eth et = new EthTest.Eth();
var encoded = et.GetTxCount(address);
encoded.Wait();
}我应该如何从单元测试中调用GetTxCount来获得实际结果。
我已经使用了"wait“命令,尽管不推荐这样做,但仍然不能让它返回结果。
单元测试失败了-它甚至没有命中Nethereum调用的API。
发布于 2018-01-17 20:56:45
您已经完成了异步测试,然后通过使用await调用GetTxCount来一直使用异步
[TestMethod]
public async Task TestMethodAsync() {
string address = "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae";
var et = new EthTest.Eth();
var encoded = await et.GetTxCount(address);
}既然GetTxCount只是返回任务,那么就没有必要在方法中等待它。
重构为
public Task<HexBigInteger> GetTxCount(string address) {
return web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(address);
}或
public async Task<HexBigInteger> GetTxCount(string address) {
var result = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(address).ConfigureAwait(false);
return result.
}https://stackoverflow.com/questions/48301368
复制相似问题