Reputation: 3968
I have the following (simplified) controller:
public async Task<IHttpActionResult> Profile(UpdateProfileModelAllowNulls modelNullable)
{
ServiceResult<ProfileModelDto> result = await _profileService.UpdateProfile(1);
return Ok(result);
}
And:
public async Task<ServiceResult<ProfileModelDto>> UpdateProfile(ApplicationUserDto user, UpdateProfileModel profile)
{
//Do something...
}
and the following NUnit test:
[Test]
public async Task Post_Profile()
{
var result = _controller.Profile(new UpdateProfileModelAllowNulls() { Email = "[email protected]", DisplayName = "TestDisplay"}) as OkNegotiatedContentResult<Task<<ProfileModelDto>>;
Assert.IsNotNull(result);
}
In my NUnit test, I am trying to check for an Ok result using this tutorial https://www.asp.net/web-api/overview/testing-and-debugging/unit-testing-with-aspnet-web-api.
My problem is that I cannot convert to an OkNegotiatedContentResult
, i assume because I am not passing in the correct object, but I cannot see what object I should be passing in. As far as I can see, I am passing in the correct object eg: OkNegotiatedContentResult<Task<<ProfileModelDto>>;
but this does not work.
I have also tried:
var result = _controller.Profile(new UpdateProfileModelAllowNulls() { Email = "[email protected]", DisplayName = "TestDisplay"}) as OkNegotiatedContentResult<Task<IHttpActionResult>>;
But this does not work either.
Can anyone help?
Upvotes: 1
Views: 2029
Reputation: 3968
As stated by @esiprogrammer, the method is async, so I needed to add the awaiter.
I was able to fix it by doing the following:
var result = _controller.Profile(new UpdateProfileModelAllowNulls() { Email = "[email protected]", DisplayName = "TestDisplay"});
var okResult = await result as OkNegotiatedContentResult<ServiceResult<ProfileModelDto>>;
I have accepted @esiprogrammer answer as he answered the question correctly, and also before me
Upvotes: 2
Reputation: 1438
You controller is Async so you should call it like:
var result = (_controller.Profile(new UpdateProfileModelAllowNulls() { Email = "[email protected]", DisplayName = "TestDisplay"}).GetAwaiter().GetResult()) as OkNegotiatedContentResult<ProfileModelDto>;
Upvotes: 2