Nethereum C# Unit Test GetTransactionCount

0 votes

Nethereum uses an Async method to get the TransactionCount of an address.

I have put the method into a async task:

public  async Task<object>  GetTxCount(string address)
{
  return await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(address).ConfigureAwait(false);
}

And attempting to test it with...

[TestMethod]
public async Task TestMethodAsync()
{
  string address = "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae";
  EthTest.Eth et = new EthTest.Eth();
  var encoded = et.GetTxCount(address);
  encoded.Wait();     
}

How should I call the GetTxCount from a unit test to get the actual result?

I have used the "wait" command even though it is not recommended, but still cannot get it to return a result.

The Unit test bombs out - it does not even hit the API that Nethereum calls.

Sep 26, 2018 in Blockchain by digger
• 26,600 points
157 views

1 answer to this question.

0 votes

You have already made the test async then use async all the way through by using await to call GetTxCount

[TestMethod]
public async Task TestMethodAsync() {
    string address = "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae";
    var et = new EthTest.Eth();
    var encoded = await et.GetTxCount(address);
}

GetTxCount is just returning the task. So you dont need to await it in the method.

Refactor to

public Task<HexBigInteger>  GetTxCount(string address) {
    return  web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(address);
}

or

public async Task<HexBigInteger>  GetTxCount(string address) {
    var result = await web3.Eth.Transactions.GetTransactionCount.SendRequestAsync(address).ConfigureAwait(false);
    return result.
}

Reference Async/Await - Best Practices in Asynchronous Programming

answered Sep 26, 2018 by slayer
• 29,240 points

Related Questions In Blockchain

0 votes
1 answer

How do I withdraw the balance from a contract on Ethereum test blockchain?

Your need to improvise your code a ...READ MORE

answered Aug 3, 2018 in Blockchain by Perry
• 17,090 points
98 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,810 points
188 views
0 votes
1 answer
0 votes
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured by TLS encryption in hashgraph architecture-hashgraph, ...READ MORE

answered Aug 6, 2018 in Blockchain by ariaholic
• 7,340 points
103 views
0 votes
1 answer

XMLHttpRequest is not defined while sending request to blockchain service via C#

use post data in url link and ...READ MORE

answered Sep 20, 2018 in Blockchain by slayer
• 29,240 points
85 views
0 votes
1 answer

Test ethereum Event Logs with truffle

You are passing tx hash into done() ...READ MORE

answered Sep 21, 2018 in Blockchain by slayer
• 29,240 points
365 views