4

I am new to C# unit testing and have to test if the method is working properly.

Here is what I have so far:

  public async Task<IHttpActionResult> Post(API_FIRM_LINK aPI_FIRM_LINK)
    {
        db.API_FIRM_LINK.Add(aPI_FIRM_LINK);
        await db.SaveChangesAsync();

        return Created(aPI_FIRM_LINK);
    }

test method: Not really sure if I am on the right path If someone could provide an example based on my test

     public async Task PostTest()

    {
        ////Arrange
        API_FIRM_LINKController controller = new API_FIRM_LINKController();

        API_FIRM_LINK aPI_FIRM_LINK = null;

        IHttpActionResult expectedResult = await controller.Post(aPI_FIRM_LINK);
        //act

        IHttpActionResult result = await controller.Post(API_FIRM_LINK, aPI_FIRM_LINK);


        ////Assert
        IComparer<IHttpActionResult> comparer = new IHttpActionResultComparer();
       // Assert.IsTrue(comparer.Equals(expectedResult, result));

        Assert.IsNotNull(result);
        Console.Write(result);
6
  • 1
    Don't use async void. Commented Mar 11, 2019 at 17:24
  • What is the issue you are facing here? Commented Mar 11, 2019 at 17:25
  • Thanks.. To be honest I am new to this - I am trying to set up a test and not really sure how to approach a method that check if its working properly Commented Mar 11, 2019 at 17:28
  • This SO answer provides some insight as to why async void won't work in unit tests Commented Mar 11, 2019 at 17:43
  • Console result = System.Threading.Tasks.Task'1{System.Web.Http.IHttoActionResult ] so far Commented Mar 11, 2019 at 19:02

2 Answers 2

7

If you use a modern version of Microsoft.VisualStudio.TestTools.UnitTesting you can use an async test method, like you do in your question.

If you want to Test whether your Post function returns the expected data, do the following:

[TestMethod()]
public async Task PostTestAsync()
{
    var controller = new API_FIRM_LINKController();
    // TODO: do some preparations, so you can expect a specific return value
    IHttpActionResult expectedResult = ...

    // call PostAsync and await for it to finish
    Task taskPost =  controller.PostAsync(API_FIRM_LINK, aPI_FIRM_LINK);
    IHttpActionResult result = await taskPost;

    // of course this can be done in one line:
    IHttpActionResult result = await controller.PostAsync(API_FIRM_LINK, aPI_FIRM_LINK);

    // compare whether result equals expectedResult
    // for example: create a class that implements IComparer<IHttpActionResult>
    IComparer<IHttpActionResult> comparer = new IHttpActionResultComparer();
    Assert.IsTrue(comparer.Equals(expectedResult, result);
}

If you use a test suite where you can't use async tests:

[TestMethod()]
public void PostTest()
{
    var controller = new API_FIRM_LINKController();
    IHttpActionResult expectedResult = ...

    // call PostAsync and wait for it to finish
    Task taskPost =  Task.Run(() => controller.PostAsync(API_FIRM_LINK, aPI_FIRM_LINK));
    taskPost.Wait();
    IHttpActionResult result = taskPost.Result;

    // TODO: compare result with expected result
}
Sign up to request clarification or add additional context in comments.

4 Comments

I tried to make some adjustments since most of it was breaking and not able to build it. await controller.PostAsync(API_FIRM_LINK, aPI_FIRM_LINK) get the following message: API_FIRM_LINK is a type - not valid . and aPI_FIRM_LINK does not exist.
I only copy-pasted the call to method Post from the code in your question. The only change I made was rename it to PostAsync, to match the more common name of an async function. I assumed you knew how to program and could see through this small change. Of course you should use Post if your code is called Post!
I am new to it..so I am doing some research on it. Thanks for the help
This "async Task" solved my problem trying for 2 hours. Actually failed to identify where the problem is. Thank you very much Harald.
5

Never async void, basically.

[TestMethod]
public async Task PostTest() {...}

If your testing framework doesn't support Task tests: change testing framework.

6 Comments

But how could I write a test that check if it works correctly or not -
@usertestREACT await Task.Delay(500); Assert.Fail(); - if it doesn't fail, it isn't working
await controller.Post(API_FIRM_LINK aPI_FIRM_LINK); error here: API_FIRM_LINK is a type which is not valid in the given context
Console result = System.Threading.Tasks.Task'1{System.Web.Http.IHttoActionResult ] so far
@MarcGravell what's the downside of using async void on a [TestMethod]?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.