public async Task TestCreate_PersonDoesNotExist()
        {
            using (ShimsContext.Create())
            {
                System.Data.Entity.Fakes.ShimDbContext.AllInstances.SetOf1 <Person>((c) =>
                {
                    return(context.People);
                });
                var userId          = 1;
                var personId        = 10;
                var phoneNumberType = new PhoneNumberType
                {
                    PhoneNumberTypeId   = PhoneNumberType.Home.Id,
                    PhoneNumberTypeName = PhoneNumberType.Home.Value
                };
                var user     = new User(userId);
                var newPhone = new NewPersonPhoneNumber(user, phoneNumberType.PhoneNumberTypeId, "1234", "123", personId, true);

                context.SetupActions.Add(() =>
                {
                    context.PhoneNumberTypes.Add(phoneNumberType);
                });
                var message = String.Format("The phoneNumberable entity with id [{0}] was not found.", personId);
                context.Revert();
                Action      a = () => service.Create(newPhone);
                Func <Task> f = () => service.CreateAsync(newPhone);

                a.ShouldThrow <ModelNotFoundException>().WithMessage(message);
                f.ShouldThrow <ModelNotFoundException>().WithMessage(message);
            }
        }
        public void TestGetPhoneNumberableEntityId()
        {
            var number       = "1234567890";
            var ext          = "123";
            var numberTypeId = PhoneNumberType.Home.Id;
            var isPrimary    = true;
            var user         = new User(1);
            var personId     = 10;
            var model        = new NewPersonPhoneNumber(user, numberTypeId, number, ext, personId, isPrimary);

            Assert.AreEqual(personId, model.GetPhoneNumberableEntityId());
        }
        public void TestConstructor()
        {
            var number       = "1234567890";
            var ext          = "123";
            var numberTypeId = PhoneNumberType.Home.Id;
            var isPrimary    = true;
            var user         = new User(1);
            var personId     = 10;
            var model        = new NewPersonPhoneNumber(user, numberTypeId, number, ext, personId, isPrimary);

            Assert.AreEqual(number, model.Number);
            Assert.AreEqual(ext, model.Extension);
            Assert.AreEqual(numberTypeId, model.PhoneNumberTypeId);
            Assert.AreEqual(isPrimary, model.IsPrimary);
            Assert.AreEqual(user.Id, model.Audit.User.Id);
            Assert.IsInstanceOfType(model.Audit, typeof(Create));
            Assert.AreEqual(personId, model.PersonId);
        }
        public async Task TestCreate_OtherPhoneNumbersPrimary()
        {
            using (ShimsContext.Create())
            {
                System.Data.Entity.Fakes.ShimDbContext.AllInstances.SetOf1 <Person>((c) =>
                {
                    return(context.People);
                });
                var    userId          = 1;
                var    personId        = 10;
                Person person          = null;
                var    phoneNumberType = new PhoneNumberType
                {
                    PhoneNumberTypeId   = PhoneNumberType.Home.Id,
                    PhoneNumberTypeName = PhoneNumberType.Home.Value
                };
                PhoneNumber primaryPhone1 = null;
                PhoneNumber primaryPhone2 = null;
                var         user          = new User(userId);
                var         newPhone      = new NewPersonPhoneNumber(user, phoneNumberType.PhoneNumberTypeId, "1234", "123", personId, true);
                Action      beforeTester  = () =>
                {
                    Assert.AreEqual(2, context.PhoneNumbers.Count());
                    Assert.IsTrue(primaryPhone1.IsPrimary.Value);
                    Assert.IsTrue(primaryPhone2.IsPrimary.Value);
                };
                Action afterTester = () =>
                {
                    Assert.AreEqual(1, context.People.Count());
                    Assert.AreEqual(2, context.PhoneNumbers.Count());
                    //the phone number is added to the person, not the context
                    Assert.IsTrue(person.PhoneNumbers.First().IsPrimary.Value);
                    Assert.IsFalse(primaryPhone1.IsPrimary.Value);
                    Assert.IsFalse(primaryPhone2.IsPrimary.Value);
                };
                context.SetupActions.Add(() =>
                {
                    person = new Person
                    {
                        PersonId = personId
                    };
                    primaryPhone1 = new PhoneNumber
                    {
                        PhoneNumberId = 10,
                        IsPrimary     = true,
                        Person        = person,
                        PersonId      = person.PersonId
                    };
                    primaryPhone2 = new PhoneNumber
                    {
                        PhoneNumberId = 20,
                        IsPrimary     = true,
                        Person        = person,
                        PersonId      = person.PersonId
                    };
                    context.People.Add(person);
                    context.PhoneNumbers.Add(primaryPhone1);
                    context.PhoneNumbers.Add(primaryPhone2);
                    context.PhoneNumberTypes.Add(phoneNumberType);
                });
                context.Revert();
                beforeTester();
                service.Create(newPhone);
                afterTester();

                context.Revert();
                beforeTester();
                await service.CreateAsync(newPhone);

                afterTester();
            }
        }
        public async Task TestCreate_CheckProperties()
        {
            using (ShimsContext.Create())
            {
                System.Data.Entity.Fakes.ShimDbContext.AllInstances.SetOf1 <Person>((c) =>
                {
                    return(context.People);
                });
                var    userId          = 1;
                var    personId        = 10;
                Person person          = null;
                var    phoneNumberType = new PhoneNumberType
                {
                    PhoneNumberTypeId   = PhoneNumberType.Home.Id,
                    PhoneNumberTypeName = PhoneNumberType.Home.Value
                };
                var    user         = new User(userId);
                var    newPhone     = new NewPersonPhoneNumber(user, phoneNumberType.PhoneNumberTypeId, "1234", "123", personId, true);
                Action beforeTester = () =>
                {
                    Assert.AreEqual(0, context.PhoneNumbers.Count());
                };
                Action afterTester = () =>
                {
                    Assert.AreEqual(1, context.People.Count());
                    Assert.AreEqual(1, person.PhoneNumbers.Count());

                    var firstPhone = person.PhoneNumbers.First();
                    Assert.AreEqual(newPhone.IsPrimary, firstPhone.IsPrimary);
                    Assert.AreEqual(newPhone.PhoneNumberTypeId, firstPhone.PhoneNumberTypeId);
                    Assert.AreEqual(newPhone.Number, firstPhone.Number);
                    Assert.AreEqual(newPhone.IsPrimary, firstPhone.IsPrimary);

                    Assert.IsNull(firstPhone.Contact);
                    Assert.IsFalse(firstPhone.ContactId.HasValue);

                    Assert.AreEqual(userId, firstPhone.History.CreatedBy);
                    Assert.AreEqual(userId, firstPhone.History.RevisedBy);
                    DateTimeOffset.Now.Should().BeCloseTo(firstPhone.History.CreatedOn, 20000);
                    DateTimeOffset.Now.Should().BeCloseTo(firstPhone.History.RevisedOn, 20000);
                };
                context.SetupActions.Add(() =>
                {
                    person = new Person
                    {
                        PersonId = personId
                    };
                    context.People.Add(person);
                    context.PhoneNumberTypes.Add(phoneNumberType);
                });
                context.Revert();
                beforeTester();
                service.Create(newPhone);
                afterTester();

                context.Revert();
                beforeTester();
                await service.CreateAsync(newPhone);

                afterTester();
            }
        }