BDDfy Executing Methods in the Wrong Order When Using the Method Naming Conventions

I was working on a unit test with BDDfy yesterday and found that my Then method was being executed first, causing the test to fail. Here's what the test class looked like:

public class ShouldLogMessageBodyOnPreReceive
{
    private Core.Observers.ReceiveObserver sut;
    private Mock<ILogger> mockLog;
    private Mock<ReceiveContext> context;
 
    public ShouldLogMessageBodyOnPreReceive()
    {
        sut = new Core.Observers.ReceiveObserver();
        mockLog = new Mock<ILogger>();
        Core.Observers.ReceiveObserver.Log = mockLog.Object;
    }
 
    void GivenTheContextIsValid()
    {
        (...my test code...)
    }
 
    void WhenTheMethodIsInvoked()
    {
        (...my test code...)
    }
    void ThenTheMessageShouldBeRetrievedFromTheContext()     {
        (...my test code...)
    }
    void AndTheMessageShouldBeLogged()     {
        (...my test code...)
    }
}


The method ThenTheMessageShouldBeRetrievedFromTheContext() was being executed first, despite the use of the Then naming convention. Changing the name to ThenWeShouldGetTheMessageBody() caused the methods to run in the correct order. Here's the updated version that worked as expected:

public class ShouldLogMessageBodyOnPreReceive
{
    private Core.Observers.ReceiveObserver sut;
    private Mock<ILogger> mockLog;
    private Mock<ReceiveContext> context;
 
    public ShouldLogMessageBodyOnPreReceive()
    {
        sut = new Core.Observers.ReceiveObserver();
        mockLog = new Mock<ILogger>();
        Core.Observers.ReceiveObserver.Log = mockLog.Object;
    }
 
    void GivenTheContextIsValid()
    {
        (...my test code...)
    }
 
    void WhenTheMethodIsInvoked()
    {
        (...my test code...)
    }
    void ThenWeShouldGetTheMessageBody()     {
        (...my test code...)
    }
    void AndTheMessageShouldBeLogged()     {
        (...my test code...)
    }
}

Comments

Popular Posts

Resolving the "n timer(s) still in the queue" Error In Angular Unit Tests

How to Get Norton Security Suite Firewall to Allow Remote Desktop Connections in Windows

How to Determine if a Column Exists in a DataReader

Fixing the "Please add a @Pipe/@Directive/@Component annotation" Error In An Angular App After Upgrading to webpack 4

Silent Renew and the "login_required" Error When Using oidc-client