public void DeleteArticle_Valid_Id() { using (TransactionScope transaction = new TransactionScope()) { articleRepository = new ArticlesRepository(); int count = articleRepository.GetAllArticles().Count(); //Count articles pre-editting of existing article SMCC.Models.Article originalArticle = new SMCC.Models.Article(); SMCC.Models.Article deleteArticle = new SMCC.Models.Article(); List <SMCC.Models.Article> existArticles = new List <SMCC.Models.Article>(); try { //Set your controller ControllerContext with fake context articleController.ControllerContext = MockContext().Object; originalArticle = articleRepository.GetArticle(1); var result = articleController.Delete(1) as ViewResult; deleteArticle = (SMCC.Models.Article)result.ViewData.Model; articleController.DeleteConfirmed(deleteArticle.ArticleId); } finally { articleRepository = new ArticlesRepository(); existArticles = articleRepository.GetAllArticles().ToList(); Assert.AreEqual(count - 1, existArticles.Count()); //Record count should be deducted by 1 after deletion CollectionAssert.DoesNotContain(existArticles, originalArticle); // Article should no longer be present in db. } } }
public async Task DeleteTest_ReturnsNotFound_WhenNoIdProvided() { // Act var result = await controller.Delete(null); // Assert var viewResult = Assert.IsType <NotFoundResult>(result); }
public async Task Delete_InvalidArticleId_ReturnsBadRequest() { //Act var result = await _articlesController.Delete(string.Empty); //Assert Assert.IsInstanceOf <BadRequestObjectResult>(result); }
public async Task Delete_Success() { // Arrange _articleRepositoryMock.Setup(m => m.DeleteAsync(1)).Returns(Task.FromResult <Article>(Builder <Article> .CreateNew().Build())); _articleRepositoryMock.Setup(m => m.GetAsync(1)).Returns(Task.FromResult <Article>(Builder <Article> .CreateNew().Build())); // Act var result = await _articlesController.Delete(1); // Assert Assert.NotNull(result); }
public async Task Delete_NotFound() { _articleRepositoryMock.Setup(m => m.GetAsync(1)).Returns(Task.FromResult <Article>(null)); var result = await _articlesController.Delete(1); Assert.NotNull(result); var notFoundResult = result as NotFoundResult; Assert.NotNull(notFoundResult); }
public async void Delete_ShouldReturn_NoContent() { // Arrange mockedRep.Setup(x => x.DeleteArticleAsync(It.IsAny <int>())).Returns(Task.FromResult(0)); // Act var result = await sut.Delete(0); var actual = result as StatusCodeResult; // Assert Assert.NotNull(actual); Assert.Equal(HttpStatusCode.NoContent, actual.StatusCode); }
public async Task ArticleDelete_Item() { // Arrange _articleRepositoryMock.Setup(m => m.GetAsync(1)).Returns(Task.FromResult <Article>(Builder <Article> .CreateNew().Build())); // Act var result = await _articlesController.Delete(1); // Assert Assert.NotNull(result); var objectResult = result as OkResult; Assert.NotNull(objectResult); }
public async Task Delete_NotExistingItem() { //Arrange _articleRepositoryMock.Setup(m => m.GetAsync(It.IsAny <int> ())).Returns(Task.FromResult <Article> (null)); //Act var result = await _articlesController.Delete(It.IsAny <int> ()); //Assert Assert.NotNull(result); var objectResult = result as NotFoundResult; Assert.NotNull(objectResult); }
public async Task Delete_NotFound() { // Arrange _articleRepositoryMock.Setup(m => m.GetAsync(0)).Returns(Task.FromResult <Article>(null)); // Act var result = await _articlesController.Delete(Builder <int> .CreateNew().Build()); // Assert Assert.NotNull(result); var objectResult = result as NotFoundResult; Assert.NotNull(objectResult); }
public async Task Delete_ReturnsNotFound() { // Arrange _articleRepositoryMock.Setup(m => m.GetAsync(0)).Returns(Task.FromResult((Article)null)); //Act var result = await _articlesController.Delete(0); // Assert Assert.NotNull(result); var objectResult = result as NotFoundResult; Assert.NotNull(objectResult); Assert.Equal(404, objectResult.StatusCode); }
public async Task Delete() { // Arrange _articleRepositoryMock.Setup(m => m.GetAsync(1)).Returns(Task.FromResult <Article>(Builder <Article> .CreateNew().Build())); // Act var result = await _articlesController.Get(1); // Assert Assert.NotNull(result); var objectResult = result as OkObjectResult; Assert.NotNull(objectResult); var obj = objectResult.Value as ArticleModel; Assert.NotNull(obj); result = await _articlesController.Delete(1); // Assert Assert.NotNull(result); var deleteResult = (OkResult)result; Assert.NotNull(deleteResult); }
public async Task Delete_Article_Delete() { // Arrange _articleRepositoryMock.Setup(m => m.GetAsync(5)).Returns(Task.FromResult <Article>(Builder <Article> .CreateNew().Build())); var articleDbSetMock = Builder <Article> .CreateNew().Build(); articleDbSetMock.Id = 5; _articleRepositoryMock.Setup(m => m.DeleteAsync(articleDbSetMock)); // Act var result = await _articlesController.Delete(5); // Assert Assert.NotNull(result); var okResult = result as OkResult; Assert.NotNull(okResult); Assert.Equal(200, okResult.StatusCode); }
public async Task CRUD_Succeeds() { var created = await SetupArticle( articleCollectionLanguageCode : "sv", articleCollectionName : "collection", articleCollectionIsPublic : false, articleName : "dummy", articleText : "Hallå!" ); var articleCollectionId = created.ArticleCollectionId; (await List(articleCollectionId)).Items.Count.ShouldBe(1); // Article in private article collection should not be accessible by another user. using (User(2)) { (await ArticlesController.Get(created.Id, new ArticleGet { })).ShouldBeOfType <NotFoundResult>(); } await ArticleCollectionsController.Update(articleCollectionId, new ArticleCollectionUpdate { Public = true }); // Article in public article collection should be accessible by another user. using (User(2)) { (await Get(created.Id)).ShouldSatisfyAllConditions( x => x.Name.ShouldBe(created.Name), x => JsonConvert .SerializeObject(x.ConlluDocument, Formatting.Indented) .ShouldMatchApproved(c => c.WithDiscriminator("CreatedConlluDocument")) ); (await ArticlesController.Update(created.Id, new ArticleUpdate { Name = "another", Text = "another", })).ShouldBeOfType <NotFoundResult>(); } var updatedName = "updated"; var upadtedText = @"Hallå värld! Han är min pappa, hon är min mamma, de är mina föräldrar. Varför vill du lära dig svenska? Det beror på att det gör det lättare att förstå vad folk säger. "; await ArticlesController.Update(created.Id, new ArticleUpdate { Name = updatedName, Text = upadtedText, }); var updated = await Get(created.Id); updated.Name.ShouldBe(updatedName); JsonConvert.SerializeObject(updated.ConlluDocument, Formatting.Indented) .ShouldMatchApproved(c => c.WithDiscriminator("UpdatedConlluDocument")); using (User(2)) { (await ArticlesController.Delete(created.Id, new ArticleDelete { })).ShouldBeOfType <NotFoundResult>(); } (await List(articleCollectionId)).Items.Count.ShouldBe(1); // Listing without specifying article collection ID should return articles from any article collection (await List(null)).Items.Count.ShouldBe(1); (await ArticlesController.Delete(created.Id, new ArticleDelete { })).ShouldBeOfType <NoContentResult>(); (await List(articleCollectionId)).Items.Count.ShouldBe(0); (await ArticlesController.Get(created.Id, new ArticleGet { })).ShouldBeOfType <NotFoundResult>(); }