Пример #1
0
        public void PersonService_AddNewWithValidDetails_ReturnsTrueAndAddsToRepo()
        {
            //arrange
            var sampleGroup = new Group {
                Id = 1, Name = "Test People", CreatedUtc = DateTime.UtcNow
            };
            var samplePeople = new List <Person>(new[]
            {
                new Person {
                    Id = 1, Forenames = "Roscoe", Surname = "Melton", Group = sampleGroup, CreatedUtc = DateTime.UtcNow
                },
                new Person {
                    Id = 2, Forenames = "Elvis Aaron", Surname = "Presley", Group = sampleGroup, CreatedUtc = DateTime.UtcNow
                },
                new Person {
                    Id = 3, Forenames = "Chesney", Surname = "Hawks", Group = sampleGroup, CreatedUtc = DateTime.UtcNow
                },
                new Person {
                    Id = 4, Forenames = "Sasha", Surname = "Baron-Cohen", Group = sampleGroup, CreatedUtc = DateTime.UtcNow
                },
                new Person {
                    Id = 5, Forenames = "Ross", Surname = "Kemp", Group = sampleGroup, CreatedUtc = DateTime.UtcNow
                }
            });

            var mockContext        = new Mock <IApplicationDbContext>();
            var mockContextFactory = new Mock <IApplicationDbContextFactory>();

            mockContextFactory.Setup(m => m.Create(It.IsAny <QueryTrackingBehavior>())).Returns(mockContext.Object);
            var mockPersonRepo = new Mock <IRepository <Person> >();

            mockPersonRepo
            .Setup(m => m.AddNew(It.IsAny <Person>(), It.IsAny <IApplicationDbContext>()))
            .Returns <Person, IApplicationDbContext>((person, _) =>
            {
                samplePeople.Add(person);
                return(true);
            });
            var mockGroupRepo = new Mock <IRepository <Group> >();

            mockGroupRepo
            .Setup(m => m.FindById(It.IsAny <int>(), It.IsAny <IApplicationDbContext>()))
            .Returns(sampleGroup);
            var personNameBuilder = new PersonNameBuilder();
            var personService     = new PersonService(mockContextFactory.Object, mockPersonRepo.Object, mockGroupRepo.Object, personNameBuilder);

            //act
            var result = personService.AddNew("Ainsley Harriot", 1);

            //assert
            Assert.IsTrue(result);
            Assert.IsTrue(samplePeople.Any(p => p.Surname == "Harriot"));
            //assert addnew called
            //assert findbyid caled
        }
Пример #2
0
        public void PersonService_AddNewWithInvalidName_ReturnsFalseAndDoesNotAddToRepo(string name, int groupId)
        {
            //arrange
            var mockContext        = new Mock <IApplicationDbContext>();
            var mockContextFactory = new Mock <IApplicationDbContextFactory>();

            mockContextFactory.Setup(m => m.Create(It.IsAny <QueryTrackingBehavior>())).Returns(mockContext.Object);
            var mockPersonRepo    = new Mock <IRepository <Person> >();
            var mockGroupRepo     = new Mock <IRepository <Group> >();
            var personNameBuilder = new PersonNameBuilder();
            var personService     = new PersonService(mockContextFactory.Object, mockPersonRepo.Object, mockGroupRepo.Object, personNameBuilder);

            //act
            var result = personService.AddNew(name, groupId);

            //assert
            Assert.IsFalse(result);
            //assert groupRepo.FindById() NOT called
            //assert personRepo.AddNew() NOT called
        }