Beispiel #1
0
        public void Fetch()
        {
            var generator = new RandomObjectGenerator();
            var userName  = generator.Generate <string>();

            var pollComment = new PollCommentData
            {
                Comment  = EntityCreator.Create <MVPollComment>(),
                UserName = userName
            };

            var childPollComment = new PollCommentData
            {
                Comment = EntityCreator.Create <MVPollComment>(_ =>
                {
                    _.ParentCommentID = pollComment.Comment.PollCommentID;
                    _.PollID          = pollComment.Comment.PollID;
                    _.UserID          = pollComment.Comment.UserID;
                }),
                UserName = userName
            };

            var comments = new List <PollCommentData> {
                pollComment, childPollComment
            };

            var newPollComment = Mock.Of <IPollComment>();

            var pollCommentFactoryCount = 0;
            var pollCommentFactory      = new Mock <IObjectFactory <IPollComment> >(MockBehavior.Strict);

            pollCommentFactory.Setup(_ => _.FetchChild(childPollComment, comments))
            .Callback <object[]>(_ => pollCommentFactoryCount++)
            .Returns(newPollComment);

            var pollComments = new Mock <IPollCommentCollection>(MockBehavior.Strict);

            pollComments.Setup(_ => _.Add(newPollComment));
            pollComments.Setup(_ => _.SetParent(It.IsAny <PollComment>()));
            pollComments.SetupGet(_ => _.EditLevel).Returns(0);

            var pollCommentsFactoryCount = 0;
            var pollCommentsFactory      = new Mock <IObjectFactory <IPollCommentCollection> >(MockBehavior.Strict);

            pollCommentsFactory.Setup(_ => _.FetchChild())
            .Callback(() => pollCommentsFactoryCount++)
            .Returns(pollComments.Object);

            var builder = new ContainerBuilder();

            builder.Register <IObjectFactory <IPollComment> >(_ => pollCommentFactory.Object);
            builder.Register <IObjectFactory <IPollCommentCollection> >(_ => pollCommentsFactory.Object);
            builder.Register <IEntities>(_ => Mock.Of <IEntities>());

            using (new ObjectActivator(builder.Build(), new ActivatorCallContext())
                   .Bind(() => ApplicationContext.DataPortalActivator))
            {
                var result = DataPortal.FetchChild <PollComment>(pollComment, comments);
                Assert.AreEqual(pollComment.Comment.CommentDate, result.CommentDate);
                Assert.AreEqual(pollComment.Comment.CommentText, result.CommentText);
                Assert.AreEqual(pollComment.Comment.PollCommentID, result.PollCommentID);
                Assert.AreEqual(pollComment.Comment.UserID, result.UserID);
                Assert.AreEqual(pollComment.UserName, result.UserName);
                Assert.AreSame(pollComments.Object, result.Comments);
            }

            pollCommentsFactory.VerifyAll();
            pollComments.VerifyAll();
            pollCommentFactory.VerifyAll();
        }
Beispiel #2
0
		public void Fetch()
		{
			var generator = new RandomObjectGenerator();
			var userName = generator.Generate<string>();

			var pollComment = new PollCommentData
			{
				Comment = EntityCreator.Create<MVPollComment>(),
				UserName = userName
			};

			var childPollComment = new PollCommentData
			{
				Comment = EntityCreator.Create<MVPollComment>(_ =>
				{
					_.ParentCommentID = pollComment.Comment.PollCommentID;
					_.PollID = pollComment.Comment.PollID;
					_.UserID = pollComment.Comment.UserID;
				}),
				UserName = userName
			};

			var comments = new List<PollCommentData> { pollComment, childPollComment };

			var newPollComment = Mock.Of<IPollComment>();

			var pollCommentFactoryCount = 0;
			var pollCommentFactory = new Mock<IObjectFactory<IPollComment>>(MockBehavior.Strict);
			pollCommentFactory.Setup(_ => _.FetchChild(childPollComment, comments))
				.Callback<object[]>(_ => pollCommentFactoryCount++)
				.Returns(newPollComment);

			var pollComments = new Mock<IPollCommentCollection>(MockBehavior.Strict);
			pollComments.Setup(_ => _.Add(newPollComment));
			pollComments.Setup(_ => _.SetParent(It.IsAny<PollComment>()));
			pollComments.SetupGet(_ => _.EditLevel).Returns(0);

			var pollCommentsFactoryCount = 0;
			var pollCommentsFactory = new Mock<IObjectFactory<IPollCommentCollection>>(MockBehavior.Strict);
			pollCommentsFactory.Setup(_ => _.FetchChild())
				.Callback(() => pollCommentsFactoryCount++)
				.Returns(pollComments.Object);

			var builder = new ContainerBuilder();
			builder.Register<IObjectFactory<IPollComment>>(_ => pollCommentFactory.Object);
			builder.Register<IObjectFactory<IPollCommentCollection>>(_ => pollCommentsFactory.Object);
			builder.Register<IEntities>(_ => Mock.Of<IEntities>());

			using (new ObjectActivator(builder.Build()).Bind(() => ApplicationContext.DataPortalActivator))
			{
				var result = DataPortal.FetchChild<PollComment>(pollComment, comments);
				Assert.AreEqual(pollComment.Comment.CommentDate, result.CommentDate);
				Assert.AreEqual(pollComment.Comment.CommentText, result.CommentText);
				Assert.AreEqual(pollComment.Comment.PollCommentID, result.PollCommentID);
				Assert.AreEqual(pollComment.Comment.UserID, result.UserID);
				Assert.AreEqual(pollComment.UserName, result.UserName);
				Assert.AreSame(pollComments.Object, result.Comments);
			}

			pollCommentsFactory.VerifyAll();
			pollComments.VerifyAll();
			pollCommentFactory.VerifyAll();
		}