public async Task ShouldPostAuthors() { Author author = new Author { Name = "Comedy" }; ObjectResult responsePost = (ObjectResult)await _controller.Post(author); Author respPost = (Author)responsePost.Value; ObjectResult responseGet = (ObjectResult)await _controller.Get(respPost.Id); Author respGet = (Author)responsePost.Value; var responseGetAll = (ObjectResult)await _controller.GetAll(); var respGetAll = responseGetAll.Value.GetType() .GetProperty("Count") .GetValue(responseGetAll.Value); Assert.AreEqual(200, (int)responsePost.StatusCode); Assert.AreEqual(200, (int)responseGet.StatusCode); Assert.AreEqual(respGet.Id, respPost.Id); Assert.AreEqual(1, respGetAll); }
public void GetAuthors_ShouldReturnAllAuthors() { //Arrange var authors = SetupAuthors(); if (authors == null) { Assert.Fail("Authors collection is null"); } //Create Mock mock.Setup(x => x.GetAuthors()).Returns(authors); var authorController = new AuthorController(mock.Object); UnitTestHelper.SetupControllerForTests(authorController); //Act var result = authorController.Get(); IEnumerable <AuthorDTO> resultAurhors; result.TryGetContentValue(out resultAurhors); //Assert Assert.IsNotNull(resultAurhors); }
public void GetAuthor_ShouldReturnFirstAuthor() { //Author var author = SetupAuthors().FirstOrDefault(s => s.AuthorId.Equals(1)); if (author == null) { Assert.Fail("Author is null"); } //Create Mock mock.Setup(x => x.GetAuthor(1)).Returns(author); //Act var authorController = new AuthorController(mock.Object); UnitTestHelper.SetupControllerForTests(authorController); var result = authorController.Get(1); //J. K. Rowling AuthorDTO responseAuthorDto; result.TryGetContentValue(out responseAuthorDto); //Assert Assert.IsNotNull(responseAuthorDto); Assert.AreSame(author.Name, responseAuthorDto.Name); }
public void Test6() { List <Author> authors = new List <Author>() { new Author() { Name = "A1" }, new Author() { Name = "B2" }, new Author() { Name = "B3" } }; database = GetDatabase(authors); var service = new AuthorController(database, _logger); var result = service.Get("B2"); Assert.AreEqual(200, result.StatusCode); var author = (Author)result.Value; Assert.AreEqual("B2", author.Name); }
public void Test5() { List <Author> authors = new List <Author>() { new Author() { Name = "A1" }, new Author() { Name = "B2" }, new Author() { Name = "B3" } }; database = GetDatabase(authors); var service = new AuthorController(database, _logger); var result = service.Get("B400"); Assert.AreEqual(404, result.StatusCode); Assert.AreEqual("No authors with this name found", result.Value); }
public void Test3() { List <Author> authors = new List <Author>() { new Author() { Name = "A1" }, new Author() { Name = "B2" }, new Author() { Name = "B3" } }; database = GetDatabase(authors); var service = new AuthorController(database, _logger); var result = service.Get(); Assert.AreEqual(200, result.StatusCode); var list = ((DbSet <Author>)result.Value).ToList(); Assert.AreEqual(authors.Count, list.Count); for (int i = 0; i < authors.Count; i++) { Assert.AreEqual(authors[i].Name, list[i].Name); } }
public async void Task_GetAuthorById_Return_OkResult() { var controller = new AuthorController(context); var AuthorId = 1; var data = await controller.Get(AuthorId); Assert.IsType <OkObjectResult>(data); }
public async void Task_GetAuthorById_Return_NotFound() { var controller = new AuthorController(context); var AuthorId = 6; var data = await controller.Get(AuthorId); Assert.IsType <NotFoundResult>(data); }
public async void NotExistAuthorIdTest() { AuthorController authorController = new AuthorController(new AuthorRepository(_dataContext)); var result = (OkObjectResult)await authorController.Get(1); Assert.Null(result.Value); }
public void Test1() { var service = new AuthorController(null, _logger); var result = service.Get(); Assert.AreEqual(500, result.StatusCode); Assert.AreEqual("Problem with database while getting", result.Value); }
public void Test2() { var service = new AuthorController(database, _logger); var result = service.Get(); Assert.AreEqual(404, result.StatusCode); Assert.AreEqual("No authors found", result.Value); }
public void AuthorController_GetOne_NotFound() { var mock = new Mock <IAuthorRepository>(); mock.Setup(m => m.Get(It.IsAny <int>())).Returns(default(Author)); var authorController = new AuthorController(mock.Object); var result = authorController.Get(999); Assert.IsInstanceOfType(result, typeof(NotFoundResult)); }
public async void Task_Get_All_Publications_Return_OkResult() { //Arrange var controller = new AuthorController(context); //Act var data = await controller.Get(); //Assert Assert.IsType <OkObjectResult>(data); }
public void AuthorController_GetAll_RepositoryException() { var mock = new Mock <IAuthorRepository>(); mock.Setup(m => m.GetAll()).Throws(new Exception()); var authorController = new AuthorController(mock.Object); var result = authorController.Get(); Assert.IsInstanceOfType(result, typeof(ExceptionResult)); }
public void AuthorController_GetOne() { var mock = new Mock <IAuthorRepository>(); mock.Setup(m => m.Get(It.IsAny <int>())).Returns(_testAuthor); var authorController = new AuthorController(mock.Object); var result = authorController.Get(0) as OkNegotiatedContentResult <Author>; Assert.IsNotNull(result); mock.Verify(m => m.Get(It.IsAny <int>())); Assert.AreEqual(result.Content.FirstName, "Александр"); }
public async void UpdateAuthorTest() { AuthorController authorController = new AuthorController(new AuthorRepository(_dataContext)); var author = (Author)((OkObjectResult)await authorController.GetAuthorByName("TesteController")).Value; author.Name = "Daniel"; await authorController.Put(author); var result = (OkObjectResult)await authorController.Get("Daniel"); Assert.NotNull(result.Value); }
public async Task GetReturnsAuthorWithSameId() { //Arrange _authorRepositoryMock.Setup(x => x.GetAsync(1)).Returns(Task.FromResult <Author>(Builder <Author> .CreateNew().With(x => x.Id = 1).Build())); //Act var result = await _authorController.Get(1); //Assert Assert.NotNull(result); var objResult = result as OkObjectResult; Assert.NotNull(objResult); var content = objResult.Value as AuthorModel; Assert.NotNull(content); Assert.Equal(1, content.Id); }
public async void TaskGetAuthorById_Return_BadRequestResult() { //Arrange var controller = new AuthorController(context); int?id = null; //Act var data = await controller.Get(id); //Assert Assert.IsType <BadRequestResult>(data); }
public async Task ShouldPostAuthors() { //Arrange var author = CreateAuthor(); //Act var postResponse = (ObjectResult)await _controller.Post(author); author = (AuthorEntity)postResponse.Value; var getResponse = (ObjectResult)await _controller.Get(author.Id); var authorResponse = (AuthorEntity)getResponse.Value; var getAllResponse = (ObjectResult)await _controller.GetAll(); var authorList = (IEnumerable <AuthorEntity>)getAllResponse.Value; //Assert Assert.AreEqual(200, postResponse.StatusCode); Assert.AreEqual(200, getResponse.StatusCode); Assert.AreEqual(author.Id, authorResponse.Id); Assert.AreEqual(1, authorList.Count()); }
public async Task Get_MockGetByIdAsync_ThrowsSqlException() { //Arrange var mockAuthorService = new Mock <IAuthorService>(); mockAuthorService.Setup(m => m.GetByIdAsync(It.IsAny <int>())) .ThrowsAsync(MakeSqlException()); var authorController = new AuthorController(mockAuthorService.Object); //Act var actionResult = await authorController.Get(1); //Assert Assert.IsAssignableFrom <BadRequestObjectResult>(actionResult); }
public void AuthorController_GetAll() { var mock = new Mock <IAuthorRepository>(); mock.Setup(m => m.GetAll()).Returns(new List <Author> { _testAuthor }); var authorController = new AuthorController(mock.Object); var result = authorController.Get() as OkNegotiatedContentResult <IEnumerable <Author> >; Assert.IsNotNull(result); mock.Verify(m => m.GetAll()); Assert.AreEqual(result.Content.Count(), 1); Assert.AreEqual(result.Content.ElementAt(0).FirstName, "Александр"); }
public async void Task_GetAuthorById_Return_MatchedData() { //Arrange var controller = new AuthorController(context); var AuthorId = 2; //Act var data = await controller.Get(AuthorId); //Assert Assert.IsType <OkObjectResult>(data); var okResult = data.Should().BeOfType <OkObjectResult>().Subject; var author = okResult.Value.Should().BeAssignableTo <Author>().Subject; Assert.Equal("Kate Bowler", author.AuthorName); Assert.Equal("123", author.AuthorImage); }
public async void Task_GetAuthorById_Return_MatchedData() { //Arrange var controller = new AuthorController(context); var AuthorId = 2; //Act var data = await controller.Get(AuthorId); //Assert Assert.IsType <OkObjectResult>(data); var okResult = data.Should().BeOfType <OkObjectResult>().Subject; var author = okResult.Value.Should().BeAssignableTo <Author>().Subject; Assert.Equal("Paulo Coelho", author.AuthorName); Assert.Equal("https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Paulo_Coelho_nrkbeta.jpg/330px-Paulo_Coelho_nrkbeta.jpg", author.AuthorImage); }
public async void Task_Get_All_Authors_Return_NotFound() { //Arrange var controller = new AuthorController(context); //Act var data = await controller.Get(); data = null; //Assert if (data != null) { Assert.IsType <OkObjectResult>(data); } else { Assert.Equal(data, null); } }
public void Test8() { List <Author> authorsNew = new List <Author>() { new Author() { Name = "A1" }, new Author() { Name = "B2" }, new Author() { Name = "B3" } }; List <Author> authors = new List <Author>(); database = GetDatabase(authors); var service = new AuthorController(database, _logger); foreach (var author in authorsNew) { var code = service.Post(author); Assert.AreEqual(200, code.StatusCode); Assert.AreEqual("Succesful adding", code.Value); } var result = service.Get(); Assert.AreEqual(200, result.StatusCode); Assert.AreEqual(authorsNew.Count, authors.Count); for (int i = 0; i < authorsNew.Count; i++) { Assert.AreEqual(authorsNew[i].Name, authors[i].Name); } }
public async Task Test2() { ac = new AuthorController(_loggerA, authorService); var result = await ac.Get("Author"); }