Ejemplo n.º 1
0
        public void GIVEN_ScanNotCalled_WHEN_DeltaApplyCollections_THEN_ExceptionThrown()
        {
            //Arrange
            Setup();

            var author         = new Author();
            var classUnderTest = new Delta <Author>(_configuration.Object);

            //Act/Assert
            var ex = Assert.Throws <Exception>(() => classUnderTest.ApplyCollections(author));

            Assert.Equal("Scan must be called before this method", ex.Message);
        }
Ejemplo n.º 2
0
        public void GIVEN_RelatedResourceCollection_WHEN_DeltaApply_THEN_ValuesApplied()
        {
            //Arrange
            Setup();

            var articleAuthorRelatedProperty           = new Mock <IPropertyHandle>(MockBehavior.Strict);
            Action <Article, ICollection> actionSetter = (o, p) => { ((Article)o).Comments = (IList <Comment>)p; };
            Func <Article, ICollection>   actionGetter = (o) => null;

            articleAuthorRelatedProperty.SetupGet(o => o.SetterDelegate).Returns((Delegate)actionSetter);
            articleAuthorRelatedProperty.SetupGet(o => o.GetterDelegate).Returns((Delegate)actionGetter);
            articleAuthorRelatedProperty.SetupGet(o => o.Type).Returns(typeof(List <Comment>));

            var articleAuthorRelationship = new Mock <IRelationshipMapping>(MockBehavior.Strict);

            articleAuthorRelationship.SetupGet(o => o.IsCollection).Returns(true);
            articleAuthorRelationship.SetupGet(o => o.RelationshipName).Returns("comments");
            articleAuthorRelationship.SetupGet(o => o.RelatedProperty).Returns(articleAuthorRelatedProperty.Object);

            _mapping.Object.Relationships.Add(articleAuthorRelationship.Object);

            var article        = new Article();
            var classUnderTest = new Delta <Article>(_configuration.Object);

            classUnderTest.CollectionDeltas =
                new Dictionary <string, ICollectionDelta>()
            {
                { "comments", new CollectionDelta <Comment>(c => c.Id)
                  {
                      Elements = new List <Comment> {
                          new Comment {
                              Id = 1
                          },
                          new Comment {
                              Id = 2
                          }
                      }
                  } }
            };

            classUnderTest.Scan();

            //Act
            classUnderTest.ApplyCollections(article);

            //Assert
            Assert.NotNull(article.Comments);
            Assert.Equal(2, article.Comments.Count);
            Assert.Equal(1, article.Comments[0].Id);
            Assert.Equal(2, article.Comments[1].Id);
        }