示例#1
0
        public void Put_ShouldUpdateDataInRepo_WhenValidUpdateRequestIsSent()
        {
            // Arrange
            var dataRepoMock = new Mock <ILearningDataRepo>(MockBehavior.Strict);
            var learningData = new LearningDataDto()
            {
                Id          = DefaultLearningData.First().Id,
                Name        = "This is an updated name",
                Description = DefaultLearningData.First().Description,
                ImageData   = DefaultLearningData.First().ImageData,
                Number      = DefaultLearningData.First().Number
            };

            // Expect the controller to test if the data set exists before trying to update it
            dataRepoMock.Setup(repo => repo.Retrieve(learningData.Id)).Returns(DefaultLearningData.First());
            dataRepoMock.Setup(repo => repo.Update(learningData));
            var sut = new Controllers.LearningDataController(dataRepoMock.Object);

            // Act
            var result   = sut.Put(learningData.Id, learningData);
            var okResult = result as OkResult;

            // Assert
            Assert.NotNull(okResult);
            VerifyMock(dataRepoMock);
        }
示例#2
0
        public void Post_ShouldStoreDataInRepo_WhenPostingValidLearningData()
        {
            // Arrange
            var dataRepoMock         = new Mock <ILearningDataRepo>(MockBehavior.Strict);
            var expectedPrimaryKey   = 5;
            var expectedResultObject = new LearningDataDto()
            {
                Name        = NewLearningDataTemplate.Name,
                Description = NewLearningDataTemplate.Description,
                Id          = expectedPrimaryKey,
                Number      = NewLearningDataTemplate.Number,
                ImageData   = NewLearningDataTemplate.ImageData,
                ImageTitle  = NewLearningDataTemplate.ImageTitle
            };

            dataRepoMock.Setup(repo => repo.Add(NewLearningDataTemplate)).Returns(expectedPrimaryKey);
            dataRepoMock.Setup(repo => repo.Retrieve(expectedPrimaryKey)).Returns(expectedResultObject);

            var sut = new Controllers.LearningDataController(dataRepoMock.Object);

            // Act
            var result         = sut.Post(NewLearningDataTemplate);
            var specificResult = result as CreatedAtActionResult;

            // Assert
            Assert.NotNull(specificResult);
            Assert.Equal("Get", specificResult.ActionName);
            Assert.Equal(StatusCodes.Status201Created, specificResult.StatusCode);
            Assert.Equal(expectedResultObject, specificResult.Value);

            VerifyMock(dataRepoMock);
        }
示例#3
0
        public void Delete_ShouldReturnNotFound_WhenUnknownIdIsProvided()
        {
            // Arrange
            var dataRepoMock = new Mock <ILearningDataRepo>(MockBehavior.Strict);
            var invalidId    = 50000;

            dataRepoMock.Setup(repo => repo.Retrieve(invalidId)).Returns <LearningDataDto>(null);
            var sut = new Controllers.LearningDataController(dataRepoMock.Object);

            // Act
            var result         = sut.Delete(invalidId);
            var notFoundResult = result as NotFoundResult;

            // Assert
            Assert.NotNull(notFoundResult);
            VerifyMock(dataRepoMock);
        }
示例#4
0
        public void Put_ShouldReturnBadRequest_WhenUpdateRequestIsSentForIncompleteData()
        {
            // Arrange
            var dataRepoMock   = new Mock <ILearningDataRepo>(MockBehavior.Strict);
            var incompleteData = new LearningDataDto();
            var sut            = new Controllers.LearningDataController(dataRepoMock.Object);

            sut.ModelState.AddModelError("test", "test");

            // Act
            var result           = sut.Put(DefaultLearningData.First().Id, incompleteData);
            var badRequestResult = result as BadRequestResult;

            // Assert
            Assert.NotNull(badRequestResult);
            VerifyMock(dataRepoMock);
        }
示例#5
0
        public void Post_ShouldReturnBadRequest_WhenPostingInvalidLearningData()
        {
            // Arrange
            var dataRepoMock = new Mock <ILearningDataRepo>(MockBehavior.Strict);

            var sut = new Controllers.LearningDataController(dataRepoMock.Object);

            sut.ModelState.AddModelError("RequiredAttributeMissing", "Required attributes were missing on the request");

            // Act
            var result           = sut.Post(new LearningDataDto());
            var badRequestResult = result as BadRequestResult;

            // Assert
            Assert.NotNull(badRequestResult);
            VerifyMock(dataRepoMock);
        }
示例#6
0
        public void Delete_ShouldDeleteLearningData_WhenValidIdIsProvided()
        {
            // Arrange
            var dataRepoMock      = new Mock <ILearningDataRepo>(MockBehavior.Strict);
            var validLearningData = DefaultLearningData.First();

            dataRepoMock.Setup(repo => repo.Retrieve(validLearningData.Id)).Returns(validLearningData);
            dataRepoMock.Setup(repo => repo.Remove(validLearningData.Id));
            var sut = new Controllers.LearningDataController(dataRepoMock.Object);

            // Act
            var result   = sut.Delete(validLearningData.Id);
            var okResult = result as OkResult;

            // Assert
            Assert.NotNull(okResult);
            VerifyMock(dataRepoMock);
        }
示例#7
0
        public void Get_ShouldReturnNotFoundResult_WhenRequestingLearningDataForInvalidId()
        {
            // Arrange
            var invalidId = 5000;

            var dataRepoMock = new Mock <ILearningDataRepo>(MockBehavior.Strict);

            dataRepoMock.Setup(obj => obj.Retrieve(invalidId)).Returns <LearningDataDto>(null);

            var sut = new Controllers.LearningDataController(dataRepoMock.Object);

            // Act
            var result         = sut.Get(invalidId);
            var notFoundResult = result as NotFoundResult;

            // Assert
            Assert.NotNull(notFoundResult);
            VerifyMock(dataRepoMock);
        }
示例#8
0
        public void Get_ShouldReturnAllEntries_WhenRequestingAllLearningData(IEnumerable <LearningDataDto> expectedLearningData)
        {
            // Arrange
            var dataRepoMock = new Mock <ILearningDataRepo>(MockBehavior.Strict);

            dataRepoMock.Setup(obj => obj.RetrieveAll()).Returns(expectedLearningData);

            var sut = new Controllers.LearningDataController(dataRepoMock.Object);

            // Act
            var result   = sut.GetAll();
            var okResult = result as OkObjectResult;

            // Assert
            Assert.NotNull(okResult);
            var actualLearningData = okResult.Value as IEnumerable <LearningDataDto>;

            Assert.NotNull(actualLearningData);
            Assert.Equal(expectedLearningData, actualLearningData);
            VerifyMock(dataRepoMock);
        }
示例#9
0
        public void Get_ShouldReturnValidResult_WhenRequestingLearningDataForValidId()
        {
            // Arrange
            var expectedLearningData = DefaultLearningData.ElementAt(1);

            var dataRepoMock = new Mock <ILearningDataRepo>(MockBehavior.Strict);

            dataRepoMock.Setup(obj => obj.Retrieve(expectedLearningData.Id)).Returns(expectedLearningData);

            var sut = new Controllers.LearningDataController(dataRepoMock.Object);

            // Act
            var result   = sut.Get(expectedLearningData.Id);
            var okResult = result as OkObjectResult;

            // Assert
            Assert.NotNull(okResult);
            var actualLearningData = okResult.Value as LearningDataDto;

            Assert.NotNull(actualLearningData);
            Assert.Equal(expectedLearningData.Id, actualLearningData.Id);
            VerifyMock(dataRepoMock);
        }
示例#10
0
        public void Put_ShouldReturnNotFoundResult_WhenUpdateRequestIsSentForUnknownId()
        {
            // Arrange
            var dataRepoMock = new Mock <ILearningDataRepo>(MockBehavior.Strict);
            var learningData = new LearningDataDto()
            {
                Id          = 5000000,
                Name        = "Invalid",
                Description = "Invalid",
                Number      = 0,
                ImageData   = null
            };

            dataRepoMock.Setup(repo => repo.Retrieve(learningData.Id)).Returns <LearningDataDto>(null);
            var sut = new Controllers.LearningDataController(dataRepoMock.Object);

            // Act
            var result         = sut.Put(learningData.Id, learningData);
            var notFoundResult = result as NotFoundResult;

            // Assert
            Assert.NotNull(notFoundResult);
            VerifyMock(dataRepoMock);
        }