9.10.10

You gotta love Design By Contract

Code Snippet
  1. [TestMethod]
  2. [ExpectedException(typeof(TradingServiceException))]
  3. public void ShouldThrowExceptionForInvalidTrade()
  4. {
  5.     var mockTrade = new Mock<AbstractTrade>(MockBehavior.Strict);
  6.     mockTrade.SetupAllProperties();
  7.  
  8.     mockTrade.Setup(trade => trade.IsValid()).Returns(false);
  9.  
  10.     var tradeManager = new TradeManager(mockTrade.Object);
  11.     tradeManager.Execute();
  12.  
  13.     mockTrade.Verify();   
  14. }

And when the test fails, we get the following message:

TradeGeneratorTests.ShouldThrowExceptionForInvalidTrade : Failed

Test method Zainco.Commodities.Unit.Tests.TradeGeneratorTests.ShouldThrowExceptionForInvalidTrade threw exception: 
Zainco.Commodities.Exceptions.TradingServiceException: Precondition failed: Trade.IsValid() == true  Trade execution failed

at System.Diagnostics.Contracts.__ContractsRuntime.Requires<TException>(Boolean condition, String message, String conditionText) in :line 0
at Zainco.Commodities.TradingService.TradeManager.Execute() in TradeManager.cs: line 28
at Zainco.Commodities.Unit.Tests.TradeGeneratorTests.ShouldThrowExceptionForInvalidTrade() in TradeGeneratorTests.cs: line 36

Sweet!

No comments: