2

Currently I am trying to do some unit test for the Controller and the ViewModel. My Controller function is like this: enter image description here It calls a private method to get user information as a helper function: enter image description here

And store the user information in a viewModel then push the viewModel to the client-side.

The Question is how can I do uniting testing for this controller and the associated viewModel?

Here is my unit test case: enter image description here

It returns a Task, which has no viewModel data. How can I access the data related to the viewModel?

Let me know if the information I gave is inadequate.

1

2 Answers 2

3

You should await the async method and you should follow "async all the way" rule.

var result = await _controller.Index("644405",DateTime.Now);

And you have to change your unit test method as async.

Sign up to request clarification or add additional context in comments.

Comments

0

Your unit test should look like this: (NUnit example)

[Test]
public async Task ShouldGetValidViewObject_async()
{
    var result = await _controller.Index("644405", DateTime.Now);
    Assert.// your assertion goes here
}

Or like this (sync)

[Test]
public void ShouldGetValidViewObject()
{
    var result = _controller.Index("644405",DateTime.Now).Result;
    Assert.// your assertion goes here
}

Comments

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.