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,740 points
1,401 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,370 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,100 points
2,685 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,790 points
1,941 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,465 views
+1 vote
1 answer

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

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,457 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,370 points
1,213 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,370 points
1,973 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP