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
Post a Comment