public void ExecutesCreate_OnAffiliationEntity()
            {
                const int personId = 13;
                var person = new Person { RevisionId = personId, };
                var command = new CreateAffiliationCommand
                {
                    PersonId = personId,
                };
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Get<Person>()).Returns(new[] { person }.AsQueryable);
                entities.Setup(m => m.Create(It.Is(AffiliationBasedOn(command))));
                var handler = new CreateAffiliationHandler(entities.Object);

                handler.Handle(command);

                entities.Verify(m => m.Create(It.Is(AffiliationBasedOn(command))), Times.Once());
            }
            public void ThrowsArgumentNullException_WhenCommandArgIsNull()
            {
                var handler = new CreateAffiliationHandler(null);
                ArgumentNullException exception = null;
                try
                {
                    handler.Handle(null);
                }
                catch (ArgumentNullException ex)
                {
                    exception = ex;
                }

                exception.ShouldNotBeNull();
                // ReSharper disable PossibleNullReferenceException
                exception.ParamName.ShouldEqual("command");
                // ReSharper restore PossibleNullReferenceException
            }
            public void ExecutesQuery_ForPerson()
            {
                const int personId = 13;
                var command = new CreateAffiliationCommand
                {
                    PersonId = personId,
                };
                var handler = new CreateAffiliationHandler(null);
                NullReferenceException exception = null;
                try
                {
                    handler.Handle(command);
                }
                catch (NullReferenceException ex)
                {
                    exception = ex;
                }

                exception.ShouldNotBeNull();
            }
            public void CreatesAffiliation_WithEstablishmentId()
            {
                Affiliation outEntity = null;
                const int personId = 13;
                const int establishmentId = 13;
                var person = new Person
                {
                    RevisionId = personId,
                };
                var command = new CreateAffiliationCommand
                {
                    PersonId = personId,
                    EstablishmentId = establishmentId,
                };
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Get<Person>()).Returns(new[] { person }.AsQueryable);
                entities.Setup(m => m.Create(It.Is(AffiliationBasedOn(command))))
                    .Callback((Entity entity) => outEntity = (Affiliation)entity);
                var handler = new CreateAffiliationHandler(entities.Object);

                handler.Handle(command);

                outEntity.ShouldNotBeNull();
                outEntity.EstablishmentId.ShouldEqual(command.EstablishmentId);
            }
            public void CreatesAffiliation_WithNotIsDefault_WhenPersonAlreadyHasDefaultAffiliation()
            {
                Affiliation outEntity = null;
                const int personId = 13;
                var person = new Person
                {
                    RevisionId = personId,
                    Affiliations = new Collection<Affiliation>
                    {
                        new Affiliation { IsDefault = true, }
                    }
                };
                var command = new CreateAffiliationCommand
                {
                    PersonId = personId,
                };
                var entities = new Mock<ICommandEntities>(MockBehavior.Strict).Initialize();
                entities.Setup(m => m.Get<Person>()).Returns(new[] { person }.AsQueryable);
                entities.Setup(m => m.Create(It.Is(AffiliationBasedOn(command))))
                    .Callback((Entity entity) => outEntity = (Affiliation)entity);
                var handler = new CreateAffiliationHandler(entities.Object);

                handler.Handle(command);

                outEntity.ShouldNotBeNull();
                outEntity.IsDefault.ShouldBeFalse();
            }