Skip to content

Instantly share code, notes, and snippets.

@crowcoder

crowcoder/StockTest

Last active Nov 5, 2017
Embed
What would you like to do?
[TestMethod]
public void BuyWhenLastTradeWasSell_Test()
{
#region setup the stock service
decimal simulatedCurrentPrice = 8.03m;
Trade simulatedLastTrade = new Trade
{
Ticker = "ABC",
Side = "sell",
TradeDate = DateTimeOffset.Now.AddHours(-1),
TradePrice = 10.00m
};
Trade simulatedBuy = new Trade
{
Ticker = "ABC",
Side = "buy",
TradeDate = DateTimeOffset.Now,
TradePrice = 11.50m
};
//Here we simulate the current price being lower than 15% less
//than our last sell.
//This should exercise the logic of a sell only.
var stockServiceMock = new Mock<IStockService>()
.GetCurrentPrice_Mock(simulatedCurrentPrice)
.GetLastTrade_Mock(simulatedLastTrade)
.Buy_Mock(simulatedBuy);
#endregion
#region setup the log service
var logServiceMock = new Mock<ILogService>().Log_Mock("Should not get called");
#endregion
StocksLogic stocksLogic = new StocksLogic(stockServiceMock.Object, logServiceMock.Object);
Trade theBuy = stocksLogic.GetRich("ABC");
Assert.IsNotNull(theBuy);
Assert.AreEqual("buy", theBuy.Side);
Assert.AreEqual(theBuy.TradeDate, simulatedBuy.TradeDate, "Simulated buy date equals buy date");
stockServiceMock.Verify(m => m.Buy(It.IsAny<string>(), It.IsAny<decimal>()), Times.Once);
stockServiceMock.Verify(m => m.Sell(It.IsAny<string>(), It.IsAny<decimal>()), Times.Never);
//Only exception will log so validate log was not called
logServiceMock.Verify(m => m.Log(It.IsAny<string>()), Times.Never);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment