Ejemplo n.º 1
0
        public async void handlerShouldExecuteCorrectlyWhenNotAuthorized()
        {
            var noteMock = new Mock <Note>();

            noteMock.Setup(obj => obj.IsOwnedBy(It.IsAny <string>())).Returns(false);

            var notesRepo = new Mock <DbSet <Note> >();

            notesRepo.Setup(obj => obj.Find(It.IsAny <int>())).Returns(noteMock.Object);

            var context = new Mock <NotepadContext>(new DbContextOptions <NotepadContext>());

            context.SetupGet(obj => obj.Notes).Returns(notesRepo.Object);

            var handler = new UpdateNoteHandler(context.Object);

            var command = new UpdateNote(1, "mytitle", "mydescription");

            command.UserId = "myuserid";

            var result = await handler.Handle(command, new CancellationToken());

            noteMock.Verify(obj => obj.IsOwnedBy(command.UserId), Times.Once());

            notesRepo.Verify(obj => obj.Find(1), Times.Once());

            Assert.False(result);
        }
Ejemplo n.º 2
0
        public async void handlerShouldExecuteCorrectlyWhenExists()
        {
            var noteMock = new Mock <Note>();

            noteMock.Setup(obj => obj.changeTitle(It.IsAny <string>())).Verifiable();
            noteMock.Setup(obj => obj.changeDescription(It.IsAny <string>())).Verifiable();
            noteMock.Setup(obj => obj.IsOwnedBy(It.IsAny <string>())).Returns(true).Verifiable();

            var notesRepo = new Mock <DbSet <Note> >();

            notesRepo.Setup(obj => obj.Find(It.IsAny <int>())).Returns(noteMock.Object).Verifiable();

            var context = new Mock <NotepadContext>(new DbContextOptions <NotepadContext>());

            context.SetupGet(obj => obj.Notes).Returns(notesRepo.Object).Verifiable();
            context.Setup(obj => obj.MarkAsModified(It.IsAny <Note>())).Verifiable();
            context.Setup <Task <int> >(obj => obj.SaveChangesAsync(It.IsAny <CancellationToken>())).ReturnsAsync(1);

            var handler = new UpdateNoteHandler(context.Object);

            var command = new UpdateNote(1, "mytitle", "mydescription");

            command.UserId = "myuserid";

            var result = await handler.Handle(command, new CancellationToken());

            noteMock.Verify();
            notesRepo.Verify();
            context.Verify();

            Assert.True(result);
        }