Exemplo n.º 1
0
        public async void OnPost_WithRevision_VerifyServiceMethodIsCalledAndConfirmationMessageIsSet()
        {
            //Arrange
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.IsNameUsed(It.IsAny <string>())).Returns(false);
            var mockWriteService = new Mock <IWritePoemService>();

            mockWriteService.Setup(w => w.SaveNewPoem(It.IsAny <PoemDTO>(), It.IsAny <RevisionDTO>()));

            //Act
            var testModel = new NewPoemModel(mockReadService.Object, mockWriteService.Object);

            testModel.NewPoem     = PoemDTO.CreateDTOFromEntity(new PoemBuilder().Default().Build());
            testModel.NewRevision = new RevisionDTO()
            {
                Title = "title",
                Body  = "body"
            };
            var testResult = await testModel.OnPostAsync();

            //Assert
            mockWriteService.Verify(w => w.SaveNewPoem(It.IsAny <PoemDTO>(), It.IsAny <RevisionDTO>()));
            Assert.Equal(NewPoemModel.POEM_CREATED_MESSAGE, testModel.ConfirmationMessage);
        }
Exemplo n.º 2
0
        public async void OnPost_InvalidModelState_VerifyServiceMethodIsNotCalled()
        {
            //Arrange
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.IsNameUsed(It.IsAny <string>()));
            var mockWriteService = new Mock <IWritePoemService>();

            mockWriteService.Setup(w => w.SaveNewPoem(It.IsAny <PoemDTO>(), It.IsAny <RevisionDTO>()));

            //Act
            var testModel = new NewPoemModel(mockReadService.Object, mockWriteService.Object);

            testModel.ModelState.AddModelError("test", "invalid state");
            var testResult = await testModel.OnPostAsync();

            //Assert
            mockReadService.Verify(r => r.IsNameUsed(It.IsAny <string>()), Times.Never);
            mockWriteService.Verify(w => w.SaveNewPoem(It.IsAny <PoemDTO>(), It.IsAny <RevisionDTO>()), Times.Never);
        }
Exemplo n.º 3
0
        public async void OnPost_NameConflict_VerifyMethodServiceIsNotCalledAndErrorMessageIsSetCorrectly()
        {
            //Arrange
            var mockReadService = new Mock <IReadPoemService>();

            mockReadService.Setup(r => r.IsNameUsed(It.IsAny <string>())).Returns(true);
            var mockWriteService = new Mock <IWritePoemService>();

            mockWriteService.Setup(w => w.SaveNewPoem(It.IsAny <PoemDTO>(), It.IsAny <RevisionDTO>()));

            //Act
            var testModel = new NewPoemModel(mockReadService.Object, mockWriteService.Object);

            testModel.NewPoem = PoemDTO.CreateDTOFromEntity(new PoemBuilder().Default().Build());
            var testResult = await testModel.OnPostAsync();

            //Assert
            mockWriteService.Verify(w => w.SaveNewPoem(It.IsAny <PoemDTO>(), It.IsAny <RevisionDTO>()), Times.Never);

            var inputFieldName = nameof(testModel.NewPoem) + "." + nameof(testModel.NewPoem.Name);

            AssertKeyInErrorState(testModel, inputFieldName);
            AssertErrorMessage(testModel, NewPoemModel.NAME_CONFLICT_MESSAGE);
        }