While writing I will check only one class at a time. If I need to use additional classes then I will use to make sure that test is independent.

Later I want to write tests, should I just copy/paste all unit tests that use mocking and test them after removing mock objects?

Example code:

public class Event
{
    string title;
    List<Hour> hoursList = new List<Hour>();
    public Event(string title) { this.title = title; }
    public void AddHour(Hour newHour) { hoursList.Add(newHour); }
    public int HourCount() { return hoursList.Count; }
    public int TotalDuration() {
        int total = 0;
        foreach (var hour in hoursList)
            total += hour.GetDuration();
        return total;
    }
}
public class Hour
{
    string start;
    string end;
    public Hour(string start, string end) { this.start = start; this.end = end; }
    public virtual int GetDuration() { /* algorightm calculating duration in minutes */  }
}

Example test:

int expected = 165;
Event Concert = new Event("Local band concert");
Mock<Hour> ehMock = new Mock<Hour>("07:00", "08:30");
ehMock.Setup(x => x.GetDuration()).Returns(90);
Mock<Hour> ehMock2 = new Mock<Hour>("19:15", "20:30");
ehMock2.Setup(x => x.GetDuration()).Returns(75);
Concert.AddHour(ehMock.Object);
Concert.AddHour(ehMock2.Object);
Assert.AreEqual(expected, Concert.TotalDuration());

In above unit test I’m testing TotalDuration() method from Event class, the Hour class is mocked and I override the GetDuration() method. Now should I use the same test for integration testing, but without the Mock?

Maybe above example is too simple and integration testing isn’t required. If I would have more advanced unit test then the integration test of same functionality is recommended?



Source link https://sqa.stackexchange.com/questions/35111/should-integration-tests--unit-tests-that-use-mocking

LEAVE A REPLY

Please enter your comment!
Please enter your name here