Question

I have a Integration project I wish to Test. I have an external web service and I want to test that it returns a string.

I have an interface for IMyService and then an implementation - MyService.

I am attempting to write Unit Tests for this project but have never done any TDD before and am trying to use Moq for the first time.

The method I want to Test has the below signature:

 public string GetTicketId(Coordinate[] coordinates)

So far in my ServiceTest class I have the following:

[TestClass]
public class MyServiceTest
{
    [TestMethod]
    public void GetTicketId()
    {
        var mockMyService = new Mock<IMyService>();

        var coordinates = new Coordinate[1];

        for (int i = 0; i < coordinates.Length; i++)
        {
            var coord1 = new Coordinate { Id = i.ToString(), X = -110.5322, Y = 35.2108, QualityIndex = 90 };
            coordinates[i] = coord1;
        }

        //mockMyService.Setup(n => n.GetTicketId(coordinates)).Returns();

        //Assert.IsNotNull(ticketId);
    }

I am kind of stuck here - what is the correct way to pass in the co-ordinates to the mockMyService so what I want to be able to assert is that the string I get back from my External service call is not null - i.e I get a response from the External Service

Was it helpful?

Solution

It sounds like you want something like this:

    [TestMethod]
    public void GetTicketId()
    {
        var mockMyService = new Mock<IMyService>();
        mockMyService.Setup(n => n.GetTicketId(It.IsAny<Coordinate[]>))
                     .Returns("Not Null String");
        var objectUnderTest = new ObjectUnderTest(mockMyService);

        var ticketId = objectUnderTest.GetTicketId();

        Assert.IsNotNull(ticketId);
    }

Basically, you set up your fake item and pass it into the object that you are testing. You do not test an interface directly, you test something that is using the interface. The point of mocking is to avoid hitting external resources (or some might say even hitting more classes than you are testing..but Ian Cooper's talk is persuasive to not go down this path).

OTHER TIPS

You mention integration test, so if you simply want to test whether or not that the web service is returning a string, you don't need Moq. Simply set up the web service as you would normally, get the result and do this:

var ws = new WebService();
var result = ws.GetTicketId(coordinates);

Assert.IsInstanceOf<ClassName>(result);

You could also do that with the service as well:

var service = new MyService();
var result = serice.GetTicketId(coordinates);

Assert.IsInstanceOf<ClassName>(result);

Moq comes into play when you are trying to test specific functionality and the functionality depends on some external connection such as a database or webservice.

You would set up Moq against an interface that does that action... let's say you have a function called "GetUser" which has an interface it uses called "IUserRepository" and you wanted to make sure GetUser only calls the function repo.GetUser(id) once... Moq lets you do that quite nicely.

I used to not understand the whole philosophy myself but once I really got it, I started to love it. The resource(s) that really got me into it was the videos from Roy Osherove: http://artofunittesting.com/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top