public void ShouldGetAll()
        {
            List <UserNote> mockedNotes = new List <UserNote>();

            for (int i = 0; i < 10; i++)
            {
                mockedNotes.Add(new UserNote()
                {
                    Text = "note " + i,
                    HdId = Hdid,
                });
            }

            RequestResult <IEnumerable <UserNote> > expectedResult = new RequestResult <IEnumerable <UserNote> >()
            {
                ResultStatus    = Common.Constants.ResultType.Success,
                ResourcePayload = mockedNotes,
            };

            Mock <IHttpContextAccessor> httpContextAccessorMock = CreateValidHttpContext(Token, UserId, Hdid);
            Mock <INoteService>         noteServiceMock         = new Mock <INoteService>();

            noteServiceMock.Setup(s => s.GetNotes(It.IsAny <string>(), 0, 500)).Returns(expectedResult);

            NoteController service      = new NoteController(noteServiceMock.Object, httpContextAccessorMock.Object);
            var            actualResult = service.GetAll(Hdid);
            RequestResult <IEnumerable <UserNote> > actualRequestResult = (RequestResult <IEnumerable <UserNote> >)((JsonResult)actualResult).Value;

            Assert.Equal(Common.Constants.ResultType.Success, actualRequestResult.ResultStatus);
        }
        public void NoteController_GetAllNotes_CountShouldBeCorrectInt()
        {
            var result = _controller.GetAll();

            Assert.AreEqual(1, _mockService.CallCount);
            Assert.IsInstanceOfType(
                result,
                typeof(OkNegotiatedContentResult <IEnumerable <NoteListItem> >)
                );
        }
Exemple #3
0
        public void TestingCorrectResponseWithControllerAsync()
        {
            // Arrange
            var      noteServiceMock = new Mock <NoteService>(null, null);
            NoteBook notebook        = new NoteBook()
            {
                Id = 1, Title = "first notebook"
            };
            List <Note> notes = new List <Note>();

            notes.Add(new Note()
            {
                Id = 1, Text = "Hello", NoteBookId = 1, Notebook = notebook
            });
            notes.Add(new Note()
            {
                Id = 2, Text = "Hello again", NoteBookId = 1, Notebook = notebook
            });

            notebook.Notes = notes;

            noteServiceMock.Setup(service => service.GetAllNotes())
            .ReturnsAsync(notes);

            var            autoMapperMock = new Mock <IMapper>();
            List <NoteDto> noteDtos       = new List <NoteDto>();

            noteDtos.Add(new NoteDto()
            {
                Id = 1, Text = "Hello"
            });
            noteDtos.Add(new NoteDto()
            {
                Id = 2, Text = "Hello again"
            });

            autoMapperMock.Setup(mapper => mapper.Map <List <NoteDto> >(notes))
            .Returns(noteDtos);

            var loggerMock = new Mock <ILogger <NoteController> >();

            var noteController = new NoteController(logger: loggerMock.Object, mapper: autoMapperMock.Object, noteService: noteServiceMock.Object);

            // Act
            List <NoteDto> noteList = noteController.GetAll().Result;

            // Assert
            Assert.Equal(noteDtos, noteList);
        }