madvora
madvora

Reputation: 1747

MVC, MOQ Unit Testing Web API Method

I've been following a regular pattern for unit testing my Web API methods using MOQ. This time I have a controller method that's a little different and I can't figure out why the test is failing.

Here's the standard look of one of our methods. We make a call to the repository and return OK.

API Method

    [HttpPost]
    public IHttpActionResult SampleMethod(SampleModel model)
    {
        var result= _myRepository.SampleMethod(model.Field1, model.Field2);

        return Ok();
    }

I usually use the following tests for something like this.

Unit Tests

    /// <summary>
    /// Tests the SampleMethod is run
    /// </summary>
    [TestMethod]
    public void SampleMethod_Is_Run()
    {
        //Arrange
        mockRepository
          .Setup(x => x.SampleMethod(It.IsAny<string>(), It.IsAny<string>()))
          .Returns(It.IsAny<EmailItem>());  //forgot to add this the first time
        var controller = new MyController(mockRepository.Object);

        //Act
        controller.SampleMethod(It.IsAny<string>(), It.IsAny<string>());

        //Assert
        mockRepository.VerifyAll();
    }

    /// <summary>
    /// Tests the SampleMethod returns correct status code
    /// </summary>
    [TestMethod]
    public void SampleMethod_Returns_OK()
    {
        //Arrange
        mockRepository
          .Setup(x => x.SampleMethod(It.IsAny<string>(), It.IsAny<string>()))
          .Returns(It.IsAny<EmailItem>());  //forgot to add this the first time;
        var controller = new MyController(mockRepository.Object);
        controller.Request = new HttpRequestMessage();
        controller.Configuration = new HttpConfiguration();

        //Act
        var response = controller.SampleMethod(It.IsAny<string>(), It.IsAny<string>());

        //Assert
        Assert.IsInstanceOfType(response, typeof(OkResult));
    }

Now let's say I have a method like this, which calls off to another class for sending an email. Why won't those unit tests work anymore?

New API Method

    [HttpPost]
    public IHttpActionResult SampleMethod(SampleModel model)
    {
        var emailItem= _myRepository.SampleMethod(model.Field1, model.Field2);

        //With this additional code, the test will fail
        EmailSender emailSender = new EmailSender();
        emailSender.BuildEmail(emailItem.ToAddress, emailItem.Subject);

        return Ok();
    }

The error message on test fail I get is this, but there is no where to see extra exception information.

    "System.Web.Http.HttpResponseException: Processing of the HTTP request resulted in an exception.  Please see the HTTP response returned by 'Response' property of this exception for details."

Upvotes: 1

Views: 2008

Answers (1)

Roelant M
Roelant M

Reputation: 1716

you do setup you repository, but you don't return anything.

mockRepository
      .Setup(x => x.SampleMethod(It.IsAny<string>(), It.IsAny<string>()));

You should try:

 mockRepository
      .Setup(x => x.SampleMethod(It.IsAny<string>(), It.IsAny<string>())).Returns(new EmailItem{ToAddress = "", Subject = ""});

you are trying to read

emailSender.BuildEmail(emailItem.ToAddress, emailItem.Subject);

Which you don't setup, so emailSender is null.

Upvotes: 2

Related Questions