public void CreatePostShouldCreate()
        {
            //Arrange
            var journalEntry = new JournalEntry
            {
                Latitude         = "12",
                Longitude        = "-12",
                LocationOverride = "Geneva",
                WeatherSummary   = "Sunny",
                Date             = new DateTime(),
                Notes            = "caught lots of fish"
            };
            var contextOptions = new DbContextOptions <DefaultContext>();
            var mockContext    = new Mock <DefaultContext>(contextOptions);

            mockContext.Setup(x => x.Add(It.IsAny <JournalEntry>()));
            mockContext.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>()));

            var controller = new JournalEntryController(mockContext.Object);

            //Act
            var response = controller.Create(journalEntry).Result;

            //Assert
            Assert.NotNull(response);
            var viewResult = Assert.IsType <RedirectToActionResult>(response);

            Assert.Equal("Index", viewResult.ActionName);
            mockContext.Verify(x => x.Add(journalEntry));
            mockContext.Verify(x => x.SaveChangesAsync(It.IsAny <CancellationToken>()));
        }
示例#2
0
        public void CreatePageShouldRender()
        {
            //Arrange
            var controller = new JournalEntryController(null, null, null);

            //Act
            var response = controller.Create();

            //Assert
            Assert.NotNull(response);
        }
示例#3
0
        public void CreatePostShouldCreate()
        {
            //Arrange
            var journalEntry = new JournalEntry
            {
                Latitude           = "12",
                Longitude          = "-12",
                LocationOverride   = "Geneva",
                Temperature        = "Sunny",
                Precipitation      = "18",
                BarometricPressure = "21",
                Humidity           = "19",
                WindSpeed          = "32",
                WindDirection      = "15",
                Date  = new DateTime(),
                Notes = "caught lots of fish"
            };
            var user = new User
            {
                Email = "*****@*****.**"
            };
            var contextOptions      = new DbContextOptions <DefaultContext>();
            var mockContext         = new Mock <DefaultContext>(contextOptions);
            var mockIdentityContext = new Mock <IdentityContext>(new DbContextOptions <IdentityContext>());
            var mockUserStore       = new Mock <UserStore <User> >(mockIdentityContext.Object, null);
            var mockUserManager     =
                new Mock <UserManager <User> >(mockUserStore.Object, null, null, null, null, null, null, null, null);

            mockUserManager.Setup(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>())).ReturnsAsync(user);
            mockContext.Setup(x => x.Add(It.IsAny <JournalEntry>()));
            mockContext.Setup(x => x.SaveChangesAsync(It.IsAny <CancellationToken>()));

            var controller = new JournalEntryController(mockContext.Object, mockUserManager.Object, null);

            //Act
            var response = controller.Create(journalEntry).Result;

            //Assert
            Assert.NotNull(response);
            var viewResult = Assert.IsType <RedirectToActionResult>(response);

            mockUserManager.Verify(x => x.GetUserAsync(It.IsAny <ClaimsPrincipal>()), Times.Once);
            mockContext.Verify(x => x.Add(journalEntry), Times.Once);
            mockContext.Verify(x => x.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once);
            Assert.Equal("Index", viewResult.ActionName);
        }