public void ExecutesQuery_ToGetAffiliation_FromPrincipalAndEstablishmentId()
            {
                const string principalIdentityName = "*****@*****.**";
                var principal = principalIdentityName.AsPrincipal();
                var command = new UpdateMyAffiliationCommand
                {
                    Principal = principal,
                };
                var affiliation = new Affiliation
                {
                    EstablishmentId = command.EstablishmentId,
                    Person = new Person { User = new User { Name = principal.Identity.Name } },
                };
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Get<Affiliation>()).Returns(new[] { affiliation }.AsQueryable);
                var handler = new UpdateMyAffiliationHandler(entities.Object);
                NullReferenceException exception = null;

                try
                {
                    handler.Handle(command);
                }
                catch (NullReferenceException ex)
                {
                    exception = ex;
                }

                entities.Verify(m => m.Get<Affiliation>(), Times.Once());
                exception.ShouldNotBeNull();
            }
            public void ThrowsArgumentNullException_WhenCommandArgIsNull()
            {
                ArgumentNullException exception = null;
                var handler = new UpdateMyAffiliationHandler(null);
                try
                {
                    handler.Handle(null);
                }
                catch (ArgumentNullException ex)
                {
                    exception = ex;
                }

                exception.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                exception.ParamName.ShouldEqual("command");
                // ReSharper restore PossibleNullReferenceException
            }
            public void UpdatesAffiliation_WhenFieldsHaveChanged()
            {
                const string principalIdentityName = "*****@*****.**";
                var principal = principalIdentityName.AsPrincipal();
                var command = new UpdateMyAffiliationCommand
                {
                    Principal = principal,
                    EstablishmentId = 4,
                };
                var affiliation = new Affiliation
                {
                    EstablishmentId = command.EstablishmentId,
                    Person = new Person { User = new User { Name = principal.Identity.Name } },
                };
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Get<Affiliation>()).Returns(new[] { affiliation }.AsQueryable);
                entities.Setup(m => m.Update(It.Is(AffiliationBasedOn(command))));
                var handler = new UpdateMyAffiliationHandler(entities.Object);

                handler.Handle(command);

                entities.Verify(m => m.Update(It.Is(AffiliationBasedOn(command))), Times.Once());
            }
            public void IncrementsChangeCount_WhenIsClaimingStaff_IsDifferent()
            {
                Affiliation outAffiliation = null;
                const string principalIdentityName = "*****@*****.**";
                var principal = principalIdentityName.AsPrincipal();
                var command = new UpdateMyAffiliationCommand
                {
                    Principal = principal,
                    EstablishmentId = 4,
                    IsClaimingStaff = true,
                };
                var affiliation = new Affiliation
                {
                    IsClaimingStaff = false,
                    IsAcknowledged = true,
                    EstablishmentId = command.EstablishmentId,
                    Person = new Person { User = new User { Name = principal.Identity.Name } },
                };
                var affiliationBasedOnCommand = AffiliationBasedOn(command);
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Get<Affiliation>()).Returns(new[] { affiliation }.AsQueryable);
                entities.Setup(m => m.Update(It.Is(affiliationBasedOnCommand)))
                    .Callback((Entity entity) => outAffiliation = (Affiliation)entity);
                var handler = new UpdateMyAffiliationHandler(entities.Object);

                handler.Handle(command);

                command.ChangeCount.ShouldEqual(1);
                outAffiliation.IsClaimingStaff.ShouldEqual(command.IsClaimingStaff);
            }