I'm using VisualStudio 2015, .NET 4.6, Moq 4.5.2, Nunit 3.4.1 to test a WebApi2 Controller. However, I am getting a null
response object when mocking the intended controller method:
var response = actionResult as NegotiatedContentResult;
I am guessing I must be setting up my mock of the UserService
incorrectly?
My suspicion is that this part is the culprit:
userServiceMock.Setup(service => service.InsertOrUpdateUser( It.IsAny())).Returns(1);
As I am getting the following in the output window:
'((System.Web.Http.Results.OkNegotiatedContentResult)actionResult).Request' threw an exception of type 'System.InvalidOperationException'
Is the problem that I am telling Moq to expect a return value of 1, but the Put method returns OkNegotiatedContentResult
?
My questions are (possibly the same question):
1) Am I setting up my Moq correctly and
2) how do I resolve the problem so my response object is populated?
Thanks much.
Here is the Test method:
[Test]
public void Put_ShouldUpdate_User()
{
// Arrange
var userServiceMock = new Mock<IUserService>();
userServiceMock.Setup(service => service.InsertOrUpdateUser(
It.IsAny<User>())).Returns(1);
var controller = new UsersController(userServiceMock.Object);
// Act
IHttpActionResult actionResult = controller.Put(
new User()
{
Id = 1,
Name = "Joe"
});
var response = actionResult as NegotiatedContentResult<User>;
// Assert:
Assert.IsNotNull(response);
var newUser = response.Content;
Assert.AreEqual(1, newUser.Id);
Assert.AreEqual("Joe", newUser.Name);
}
Here is the UserController
method:
// PUT api/users/1
public IHttpActionResult Put(User user)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
return Ok(_userService.InsertOrUpdateUser(user));
}
Finally, the method for the UserService:
public int InsertOrUpdateUser(User user)
{
return _userRepository.InsertOrUpdateUser(user);
}
Aucun commentaire:
Enregistrer un commentaire