public void UpdateCustomerDetailsShouldCauseCustomerInDbSetToBeUpdated()
        {
            // Arrange
            var mockDPerson = Builder <DeliveryPerson> .CreateNew().Build();

            var initialList = new DeliveryPerson[] { mockDPerson };
            var mockDbSet   = new MockDbSet <DeliveryPerson>(initialList);
            var mockContext = new Mock <IContext>();

            mockContext.SetupProperty <IDbSet <DeliveryPerson> >(_ => _.DeliveryPersons)
            .SetupGet(_ => _.DeliveryPersons)
            .Returns(mockDbSet);
            mockContext.Setup(_ => _.Save())
            .Callback(mockDbSet.Save);
            Context    = mockContext.Object;
            Repository = new DeliveryPersonRepository(Context);
            var updates = Builder <DeliveryPerson> .CreateNew().And(_ => { _.Id = mockDPerson.Id; }).Build();

            // Act
            Repository.UpdateDeliveryPersonDetails(updates);
            var updatedCustomer = mockDbSet.Find(mockDPerson.Id);

            // Assert
            bool updated = mockDPerson.FirstName == updatedCustomer.FirstName &&
                           mockDPerson.Id == updatedCustomer.Id &&
                           mockDPerson.LastName == updatedCustomer.LastName;

            Assert.IsTrue(updated);
        }
Example #2
0
        public void UpdateCustomerDetailsShouldCauseCustomerInDbSetToBeUpdated()
        {
            // Arrange
            var mockCustomer = Builder <Customer> .CreateNew().Build();

            var initialList = new Customer[] { mockCustomer };
            var mockDbSet   = new MockDbSet <Customer>(initialList);
            var mockContext = new Mock <IContext>();

            mockContext.SetupProperty <IDbSet <Customer> >(_ => _.Customers)
            .SetupGet(_ => _.Customers)
            .Returns(mockDbSet);
            mockContext.Setup(_ => _.Save())
            .Callback(mockDbSet.Save);
            Context    = mockContext.Object;
            Repository = new CustomerRepository(Context);
            var updates = Builder <Customer> .CreateNew().And(_ => { _.Id = mockCustomer.Id; }).Build();

            // Act
            Repository.UpdateCustomerDetails(updates);
            var updatedCustomer = mockDbSet.Find(mockCustomer.Id);

            // Assert
            bool updated = mockCustomer.Address == updatedCustomer.Address &&
                           mockCustomer.FirstName == updatedCustomer.FirstName &&
                           mockCustomer.Id == updatedCustomer.Id &&
                           mockCustomer.LastName == updatedCustomer.LastName &&
                           mockCustomer.MobileNumber == updatedCustomer.MobileNumber;

            Assert.IsTrue(updated);
        }
        public void FindMustReturnNullWhenPredicateReturnsFalseForAllEntities()
        {
            MockDbSet <Person> persons = new MockDbSet <Person>(new[] { new Person {
                                                                            FirstName = "Bob", LastName = "Smith"
                                                                        } });

            Assert.Null(persons.Find(Guid.NewGuid()));
        }
        public void FindMustReturnEntityWhenPredicateReturnsTrue()
        {
            Person expected = new Person {
                FirstName = "Bob", LastName = "Smith"
            };
            MockDbSet <Person> persons = new MockDbSet <Person>(new[] { expected });
            Person             actual  = persons.Find(expected.Id);

            Assert.NotNull(actual);
            Assert.Equal(expected.FirstName, actual.FirstName);
        }