Skip to main content

Teaser

These tests all pass:

[Test]
[ExpectedException(typeof(AssertionException))]
public void ThrowsAssertionExceptionForWrongArgument()
{
    Expect.Call(() => Console.WriteLine("A specific string."));
    StaticClass.CodeUnderTestThatCallsConsoleWriteLine();
    Verify.AllExpectations();
}

[Test]
public void VerifiesCallWithoutActuallyPerformingTheCall()
{
    Expect.Call(() => Console.WriteLine("You shouldn't see me."));
    StaticClass.CodeUnderTestThatCallsConsoleWriteLine();
    Verify.AllExpectations();
}

[Test]
public void PerformsAlternativeAction()
{
    Expect.Call(() => Console.WriteLine("YOU SHOULDN'T SEE THIS")).IgnoreArguments()
        .Do(() => Console.WriteLine("You should see this instead."));
    StaticClass.CodeUnderTestThatCallsConsoleWriteLine();
    Verify.AllExpectations();
}

[Test]
public void IgnoresArguments()
{
    Expect.Call(() => Console.WriteLine("")).IgnoreArguments();
    StaticClass.CodeUnderTestThatCallsConsoleWriteLine();
    Verify.AllExpectations();
}

[Test]
public void InterceptsBothCalls()
{
    Expect.Call(() => Console.WriteLine("")).IgnoreArguments();
    StaticClass.CallsConsoleWriteLineTwice();
    Verify.AllExpectations();
}

[Test]
public void InterceptsOnlyOneOverload()
{
    Expect.Call(() => Console.WriteLine("IGNORED")).IgnoreArguments();
    StaticClass.CallsTwoOverloads();
    Verify.AllExpectations();
}

Where StaticClass looks like this:

public static class StaticClass
{
    public static void CodeUnderTestThatCallsConsoleWriteLine()
    {
        Console.WriteLine("You shouldn't see me.");
    }

    public static void CallsConsoleWriteLineTwice()
    {
        Console.WriteLine("You shouldn't see me.");
        Console.WriteLine("You shouldn't see me.");
    }

    public static void CallsTwoOverloads()
    {
        var characters = "You SHOULD see me.".ToCharArray();

        Console.WriteLine("You shouldn't see me.");
        Console.WriteLine(characters);
    }
}

And the console output as shown by ReSharper is this:

teaser_post

Comments