gpt4 book ai didi

c# - Nethereum C# 单元测试 GetTransactionCount

转载 作者:行者123 更新时间:2023-11-30 23:06:19 25 4
gpt4 key购买 nike

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 以获得实际结果。

我已经使用了“等待”命令,尽管不推荐,但仍然无法返回结果。

单元测试失败了——它甚至没有命中 Nethereum 调用的 API。

最佳答案

您已经将测试设为异步,然后通过使用 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.
}

引用 Async/Await - Best Practices in Asynchronous Programming

关于c# - Nethereum C# 单元测试 GetTransactionCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48301368/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com