How can I say that a method of a certain class was called before another method of another class?

Here is an example of what I have:

public class ClassToBeTestedTest
{
    private Mock<IAService> aService;
    private Mock<IAnotherService> anotherService;
    private ClassToBeTested testedClass;

    [SetUp]
    public void setup()
    {
        aService = new Mock<IAService>();
        anotherService = new Mock<IAnotherService>();
        testedClass = new ClassToBeTested(aService.Object, anotherService.Object);
    }

    [Test]
    public void ShouldCallAServiceMethodBeforeAnotherService()
    {
        testedClass.Run();
        aService.Verify(x=>x.AMethod(), Times.Once());
        anotherService.Verify(x=>x.AnotherMethod(), Times.Once());
    }
}

In this example, I just check to see if they were called, there is no sequence ...

Im considering setting a callback in those methods that add some sequence control in a test class ...

edit: I am using moq lib: http://code.google.com/p/moq/

+3
source share
3 answers

, , :

public class ClassToBeTestedTest
{
    private Mock<IAService> aService;
    private Mock<IAnotherService> anotherService;
    private ClassToBeTested testedClass;

    [SetUp]
    public void setup()
    {
        aService = new Mock<IAService>();
        anotherService = new Mock<IAnotherService>();
        testedClass = new ClassToBeTested(aService.Object, anotherService.Object);
    }

    [Test]
    public void ShouldCallAServiceMethodBeforeAnotherService()
    {
        //Arrange
        anotherService.Setup(x=>x.AnotherMethod()).Callback(()=>{
            //Assert
            aService.Verify(x=>x.AMethod(), Times.Once());
        }).Verifyable();

        //Act
        testedClass.Run();
        //Assert
        anotherService.Verify();
    }
}
+1

Record the time stamp in each of your layouts and compare them.

[Test]
public void ShouldCallAServiceMethodBeforeAnotherService()
{
    testedClass.Run();
    //Not sure about your mocking library but you should get the idea
    Assert(aService.AMethod.FirstExecutionTime 
        < anotherService.AnotherMethod.FirstExecutionTime, 
        "Second method executed before first");
}
0
source

All Articles