public void Search_API_Lookup_Test() { // Arrange IDingDb db = new DingDbTest(); var mockService = new Mock <ILookupService>(); mockService.Setup(x => x.DbLookup(It.IsAny <string>())).Returns((string x) => { return(null); }); mockService.Setup(x => x.APILookup(It.IsAny <string>())).Returns((string x) => { return(new WordModel() { word = "api-test", definitions = "api test definition" }); }); SearchController controller = new SearchController(mockService.Object); WordModel testWord = new WordModel { word = "api-test", definitions = "api test definition" }; //expected result from mocked Dictionary API call //int expectedCount = 6; //5 initial entries + intel = 6 entries // Act ViewResult result = controller.Search("api-test") as ViewResult; WordModel resultWord = result.Model as WordModel; //int testCount = db.Query<WordModel>().Count(); // Assert Assert.IsNotNull(resultWord); Assert.AreEqual(testWord.word, resultWord.word); Assert.AreEqual(testWord.definitions, resultWord.definitions); //Assert.AreEqual(expectedCount, testCount); }
public void Search_Word_Not_Found() { // Arrange IDingDb db = new DingDbTest(); ILookupService lookupService = new LookupService(db); SearchController controller = new SearchController(lookupService); string testWord = "hahahathisjibberishwillreturnnothing"; // Act ViewResult result = controller.Search(testWord) as ViewResult; // Assert Assert.IsNotNull(result); Assert.AreEqual("Word Not Found", result.ViewBag.Title); Assert.IsTrue(result.ViewBag.Message.Contains(testWord)); }
public void Search_Local_Lookup_Test() { // Arrange IDingDb db = new DingDbTest(); ILookupService lookupService = new LookupService(db); SearchController controller = new SearchController(lookupService); WordModel testWord = new WordModel { word = "test_Word_2", definitions = "test Definition 2" }; // Act ViewResult result = controller.Search("test_Word_2") as ViewResult; WordModel resultWord = result.Model as WordModel; // Assert Assert.IsNotNull(result); Assert.AreEqual(testWord.word, resultWord.word); Assert.AreEqual(testWord.definitions, resultWord.definitions); }
public void Test_Get() { // Arrange IDingDb db = new DingDbTest(); ILookupService lookupService = new LookupService(db); ValuesController controller = new ValuesController(lookupService); string invalidWord = "hahahainvalidjibberishlol"; // Act JObject localResult = controller.Get("test_Word_1"); JObject apiResult = controller.Get("intel"); JObject invalidResult = controller.Get(invalidWord); // Assert Assert.IsNotNull(localResult); Assert.IsNotNull(apiResult); Assert.IsNotNull(invalidResult); Assert.AreEqual(localResult["definitions"], "test Definition 1"); Assert.AreEqual(apiResult["definitions"], "1) intelligence "); Assert.AreEqual(invalidResult["Error"], $"Merriam Webster API returned no results for \"{invalidWord}\". Is your input valid?"); }