public TestLongToShortURL() { var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables(); Configuration = builder.Build(); controller = new UrlShortenerController(Configuration); }
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)); }