public void Index_Contains_All_Urls_By_IP() { // Arrange - Fill our model with data first. UrlShortenerModel rs = new UrlShortenerModel(){ strUrl = "string", urlList = new Url[]{ new Url{ UrlId = 0, UrlCode = "TYUR", OriginalUrl="https://fluentvalidation.com", IpAddress="127.0.0.1", PostedDate = DateTime.Now}, new Url{ UrlId = 1, UrlCode = "TwUR", OriginalUrl="https://facebook.com", IpAddress="127.0.0.2", PostedDate = DateTime.Now}, new Url{ UrlId = 2, UrlCode = "TkUR", OriginalUrl="https://youtube.com/", IpAddress="127.0.0.1", PostedDate = DateTime.Now} }.AsQueryable() }; //Assert , to determine if your model is ready Assert.AreEqual(rs.urlList.Count(), 3); Assert.AreEqual(rs.urlList.Count(x=>x.IpAddress==rs.urlList.ToList()[1].IpAddress), 1); Assert.AreEqual("TkUR", rs.urlList.ToList()[2].UrlCode); Assert.AreEqual("TwUR", rs.urlList.ToList()[1].UrlCode); Assert.AreEqual("TYUR", rs.urlList.ToList()[0].UrlCode); }
public void Test_Create_With_Validation_Error() { // Arrange and mock our Repository with ourfake data Mock<IUrlsRepository> mock = new Mock<IUrlsRepository>(); mock.Setup(u => u.Urls).Returns(new Url[] { new Url{ UrlId = 0, UrlCode = "TYUR", OriginalUrl="https://fluentvalidation.com", PostedDate = DateTime.Now}, new Url{ UrlId = 1, UrlCode = "TwUR", OriginalUrl="https://facebook.com", PostedDate = DateTime.Now}, new Url{ UrlId = 2, UrlCode = "TkUR", OriginalUrl="https://youtube.com/", PostedDate = DateTime.Now} }.AsQueryable()); UrlShortenerController.ip = "127.0.0.1"; UrlShortenerController controller= new UrlShortenerController(mock.Object); controller.ModelState.AddModelError("NoteText", "NoteText cannot be null"); UrlShortenerModel model = new UrlShortenerModel(); // Act ActionResult result = controller.ShortenURl(model); // Assert , when we encounter an error, we redirect back to the same // Shorten url controller method which is of ActionResult type. Assert.IsInstanceOfType(result, typeof(ActionResult)); }
public void Test_Create_Without_Validation_Error() { // Arrange and mock our Repository with ourfake data Mock<IUrlsRepository> mock = new Mock<IUrlsRepository>(); mock.Setup(u => u.Urls).Returns(new Url[] { new Url{ UrlId = 0, UrlCode = "TYUR", OriginalUrl="https://fluentvalidation.com", IpAddress="127.0.0.1", PostedDate = DateTime.Now}, new Url{ UrlId = 1, UrlCode = "TwUR", OriginalUrl="https://facebook.com", IpAddress="127.0.0.1", PostedDate = DateTime.Now}, new Url{ UrlId = 2, UrlCode = "TkUR", OriginalUrl="https://youtube.com/", IpAddress="127.0.0.1", PostedDate = DateTime.Now} }.AsQueryable()); UrlShortenerController.ip = "127.0.0.1"; // Set Ip UrlShortenerController controller = new UrlShortenerController(mock.Object); UrlShortenerModel model = new UrlShortenerModel(); // Act ActionResult result = controller.ShortenURl(model); // Assert , if our validation no error, it saves and return redirect to //index which is of ViewResult type. Assert.IsInstanceOfType(result, typeof(ViewResult)); }