コード例 #1
0
        public void ShouldReturnCorrectContactFromContactsRepository(int id, string expectedContactTitle)
        {
            // Arrange
            var mockedContacts = new List <Contact>
            {
                new Contact()
                {
                    Id = 2, Title = "Really important message!"
                },
                new Contact()
                {
                    Id = 4, Title = "You want to loose weight?"
                }
            };
            var mockedUnitOfWork        = new Mock <IUnitOfWork>();
            var mockedGenericRepository = new Mock <IGenericRepository <Contact> >();

            mockedGenericRepository.Setup(gr => gr.GetById(id))
            .Returns(mockedContacts.Find(p => p.Id == id));

            var contactsService = new ContactsService(mockedUnitOfWork.Object, mockedGenericRepository.Object);

            // Act
            var result = contactsService.GetContactById(id);

            // Assert
            Assert.AreEqual(expectedContactTitle, result.Title);
        }
コード例 #2
0
        public IHttpActionResult GetContactsTable(Guid id)
        {
            var contactsTable = _contactsService.GetContactById(id);

            if (contactsTable == null)
            {
                return(NotFound());
            }
            return(Ok(contactsTable));
        }
コード例 #3
0
        public JsonResult RemoveContact(int contactId)
        {
            var result = new ServiceResponse <Object>();

            var contact = _contactService.GetContactById(contactId);

            try
            {
                _contactService.RemoveContact(contact);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("RemoveContact: " + ex.Message);

                result.Message = ex.Message;
            }

            result.Result = GetAllGroupsResponse();

            return(JsonResponse(result));
        }
コード例 #4
0
        public void ShouldCallGetContactByIdMethodOfContactsRepositoryOnce(int id)
        {
            // Arrange
            var mockedUnitOfWork        = new Mock <IUnitOfWork>();
            var mockedGenericRepository = new Mock <IGenericRepository <Contact> >();

            mockedGenericRepository.Setup(gr => gr.GetAll()).Verifiable();

            var contactsService = new ContactsService(mockedUnitOfWork.Object, mockedGenericRepository.Object);

            // Act
            contactsService.GetContactById(id);

            // Assert
            mockedGenericRepository.Verify(gr => gr.GetById(id), Times.Once);
        }