public void Setup() { stockAPIService = Substitute.For <StockAPIService>(new RemoteURLReader()); stockAPIService.GetPrice(Arg.Any <string>()).Returns(150); logger = Substitute.For <Logger>(); trader = new Trader(stockAPIService, logger); }
/// <summary> /// Checks the price of a stock, and buys it if the price is not greater than the bid amount. /// </summary> /// <param name="symbol">the symbol to buy, e.g. aapl</param> /// <param name="bid">the bid amount</param> /// <returns>whether any stock was bought</returns> public bool Buy(string symbol, double bid) { double price = _stockApiService.GetPrice(symbol); bool result; if (price <= bid) { result = true; _stockApiService.Buy(symbol); _logger.Log("Purchased " + symbol + " stock at $" + bid + ", since its higher that the current price ($" + price + ")"); } else { _logger.Log("Bid for " + symbol + " was $" + bid + " but the stock price is $" + price + ", no purchase was made."); result = false; } return(result); }
[Test] // everything works public void TestGetPriceNormalValues() { remoteURLReader.ReadFromUrl(String.Format(apipath, "ibm")).Returns("{\n \"symbol\": \"ibm\",\n \"price\": 123\n}"); Assert.AreEqual(123.0d, stockAPIService.GetPrice("ibm")); }