Пример #1
0
        public void ShouldKnowHowToAddStaticPagesToTheViewBag()
        {
            var documentSession = new Mock <IDocumentSession>();

            documentSession.Setup(session => session.SaveChanges());
            var documentStore = new Mock <IDocumentStore>();

            documentStore.Setup(store => store.OpenSession()).Returns(documentSession.Object);
            var repository = new Mock <Repository>();
            var savedPages = new List <StaticPage>
            {
                new StaticPage {
                    Id = 1, Title = "test title", Content = "Hello\n=====\nWorld"
                },
                new StaticPage {
                    Id = 2, Title = "test title2", Content = "Hello\n=====\nAnother World"
                }
            };

            repository.Setup(repo => repo.List <StaticPage>()).Returns(savedPages);
            var controller = new TestBaseController(repository.Object, documentStore.Object);

            repository.Verify(repo => repo.List <StaticPage>(), Times.Never());
            controller.OnActionExecuting(null);
            var _actual = (List <StaticPage>)controller.ViewBag.StaticPages;

            Assert.AreEqual(savedPages.Count(), _actual.Count());
            Assert.AreEqual(savedPages.First(), _actual.First());
            Assert.AreEqual(savedPages.Last(), _actual.Last());
            repository.Verify(repo => repo.List <StaticPage>(), Times.Once());
        }
Пример #2
0
        public void ShouldKnowToOpenSessionOnActionExecuting()
        {
            var documentSession = new Mock <IDocumentSession>();

            documentSession.Setup(session => session.SaveChanges());
            var documentStore = new Mock <IDocumentStore>();

            documentStore.Setup(store => store.OpenSession()).Returns(documentSession.Object);
            var testBaseController = new TestBaseController(null, documentStore.Object);

            testBaseController.OnActionExecuting(null);
            documentStore.Verify(store => store.OpenSession(), Times.Once());
        }
Пример #3
0
        public void ShouldKnowToNotSaveChangesOnActionExecutedWhenExceptionPresentInContext()
        {
            var repository = new Mock<IRepository>();
            repository.Setup(repo => repo.SaveChanges());
            var documentStore = new Mock<IDocumentStore>();
            var testBaseController = new TestBaseController(documentStore.Object, repository.Object);
            var actionExecutedContext = new Mock<ActionExecutedContext>();
            actionExecutedContext.SetupGet(context => context.IsChildAction).Returns(false);
            actionExecutedContext.Setup(context => context.Exception).Returns(new Exception("test exception"));

            testBaseController.OnActionExecuted(actionExecutedContext.Object);

            repository.Verify(repo => repo.SaveChanges(), Times.Never());
        }
Пример #4
0
        public void ShouldKnowToSaveChangesOnActionExecuted()
        {
            var documentSession = new Mock <IDocumentSession>();
            var documentStore   = new Mock <IDocumentStore>();

            documentStore.Setup(store => store.OpenSession()).Returns(documentSession.Object);

            var testBaseController = new TestBaseController(null, documentStore.Object);

            testBaseController.OnActionExecuting(null);

            var actionExecutedContext = new Mock <ActionExecutedContext>();

            actionExecutedContext.SetupGet(context => context.IsChildAction).Returns(false);
            actionExecutedContext.Object.Exception = null;

            testBaseController.OnActionExecuted(actionExecutedContext.Object);

            documentSession.Verify(session => session.SaveChanges(), Times.Once());
        }