Last active
December 18, 2015 19:29
-
-
Save hjerpbakk/5833819 to your computer and use it in GitHub Desktop.
Use mocks sparingly and keep them simple! Never make a mock more complex. From http://hjerpbakk.com/blog/2013/6/21/ndc-2013-day-3.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[TestClass] | |
public class MockExample | |
{ | |
/// <summary> | |
/// An interface for an imaginary stock service. | |
/// </summary> | |
public interface IStockService | |
{ | |
double QuoteForTicker(string ticker); | |
} | |
/// <summary> | |
/// Class representing an application model. | |
/// Utilizes the IStockService in its implementation. | |
/// </summary> | |
public class SampleApplicationModel | |
{ | |
private readonly IStockService stockService; | |
public SampleApplicationModel(IStockService stockService) | |
{ | |
this.stockService = stockService; | |
} | |
public string CurrentTicker { get; set; } | |
public string LatestQuote { get; set; } | |
public void GetQuote() | |
{ | |
var quote = stockService.QuoteForTicker(CurrentTicker); | |
LatestQuote = quote.ToString(); | |
} | |
} | |
[TestMethod] | |
public void GetQuoteMustSetLatestQuoteForTicker() | |
{ | |
// Simple mock, using an appropriate tool | |
var stockServiceFake = new Mock<IStockService>(); | |
stockServiceFake.Setup(s => s.QuoteForTicker("AAPL")).Returns(700D); | |
var model = new SampleApplicationModel(stockServiceFake.Object) { CurrentTicker = "AAPL" }; | |
// Mock with internal logic | |
//var model = new SampleApplicationModel(new StockServiceFake()) { CurrentTicker = "AAPL" }; | |
model.GetQuote(); | |
Assert.AreEqual("700", model.LatestQuote); | |
} | |
[TestMethod] | |
[ExpectedException(typeof(InvalidOperationException))] | |
public void GetQuoteLFailsMustSetErrorMessageAsQuoteValue() | |
{ | |
// Simple mock, using an appropriate tool | |
var stockServiceFake = new Mock<IStockService>(); | |
stockServiceFake.Setup(s => s.QuoteForTicker("AAPL")).Throws(new InvalidOperationException()); | |
var model = new SampleApplicationModel(stockServiceFake.Object) { CurrentTicker = "AAPL" }; | |
// Mock with internal logic | |
//var model = new SampleApplicationModel(new StockServiceFake(true)) { CurrentTicker = "AAPL" }; | |
model.GetQuote(); | |
} | |
/// <summary> | |
/// Complicated mock. Hard to understand, must be maintained | |
/// together with the interface it's implementing. | |
/// </summary> | |
private class StockServiceFake : IStockService | |
{ | |
private readonly bool throwException; | |
public StockServiceFake(bool throwException = false) | |
{ | |
this.throwException = throwException; | |
} | |
public double QuoteForTicker(string ticker) | |
{ | |
if (throwException) | |
{ | |
throw new InvalidOperationException(); | |
} | |
return 700D; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment