/// <summary> /// Get stock price from iex /// </summary> /// <param name="symbol">symbol Stock symbol, for example "aapl"</param> /// <returns>the stock price</returns> public virtual double GetPrice(string symbol) { string url = String.Format(apiPath, symbol); string result = _remoteURLReader.ReadFromUrl(url); var json = JObject.Parse(result); string price = json.GetValue("price").ToString(); return(double.Parse(price)); }
[Test] // readFromURL threw an exception public void TestGetPriceServerDown() { ////arrange //_reader.ReadFromUrl("http://www.test.com/{0}").Returns(x => { throw new Exception(); }); //var aPIService = StockAPIService.Instance(); //string symbol = "aapl"; RemoteURLReader reader = RemoteURLReader.Instance(); string url = @"http://www.test.com"; ////act //aPIService.Reader = _reader; //aPIService.APIPath = "http://www.test.com/{0}"; ////assert //Assert.Throws<Exception>(()=> aPIService.GetPrice(symbol)); Assert.Throws <WebException>(() => reader.ReadFromUrl(url)); }
[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")); }