コード例 #1
0
        public void GetValueProvider_EmptyTempData_ReturnsNull() {
            // Arrange
            TempDataDictionary tempData = new TempDataDictionary();
            ControllerContext controllerContext = GetControllerContext(tempData);

            TempDataValueProviderFactory factory = new TempDataValueProviderFactory();

            // Act
            IValueProvider provider = factory.GetValueProvider(controllerContext);

            // Assert
            Assert.IsNull(provider);
        }
コード例 #2
0
        public void GetValueProvider_EmptyTempData_ReturnsNull()
        {
            // Arrange
            TempDataDictionary tempData          = new TempDataDictionary();
            ControllerContext  controllerContext = GetControllerContext(tempData);

            TempDataValueProviderFactory factory = new TempDataValueProviderFactory();

            // Act
            IValueProvider provider = factory.GetValueProvider(controllerContext);

            // Assert
            Assert.IsNull(provider);
        }
コード例 #3
0
        public void GetValueProvider_CorrectlyRetainsOrRemovesKeys()
        {
            // Arrange
            string[] expectedRetainedKeys = new[] { "retainMe" };

            TempDataDictionary tempData = new TempDataDictionary
            {
                { "retainMe", "retainMeValue" },
                { "removeMe", "removeMeValue" },
                { "previouslyRemoved", "previouslyRemovedValue" }
            };
            object dummy = tempData["previouslyRemoved"]; // mark value for removal

            ControllerContext controllerContext = GetControllerContext(tempData);

            TempDataValueProviderFactory factory = new TempDataValueProviderFactory();

            // Act
            IValueProvider      valueProvider     = factory.GetValueProvider(controllerContext);
            ValueProviderResult nonExistentResult = valueProvider.GetValue("nonExistent");
            ValueProviderResult removeMeResult    = valueProvider.GetValue("removeme");

            // Assert
            Assert.Null(nonExistentResult);
            Assert.Equal("removeMeValue", removeMeResult.AttemptedValue);
            Assert.Equal(CultureInfo.InvariantCulture, removeMeResult.Culture);

            // Verify that keys have been removed or retained correctly by the provider
            Mock <ITempDataProvider> mockTempDataProvider = new Mock <ITempDataProvider>();

            string[] retainedKeys = null;
            mockTempDataProvider
            .Setup(
                o => o.SaveTempData(controllerContext, It.IsAny <IDictionary <string, object> >())
                )
            .Callback(
                delegate(ControllerContext cc, IDictionary <string, object> d)
            {
                retainedKeys = d.Keys.ToArray();
            }
                );

            tempData.Save(controllerContext, mockTempDataProvider.Object);
            Assert.Equal(expectedRetainedKeys, retainedKeys);
        }
コード例 #4
0
        public void GetValueProvider_CorrectlyRetainsOrRemovesKeys()
        {
            // Arrange
            string[] expectedRetainedKeys = new[]
            {
                "retainMe"
            };

            TempDataDictionary tempData = new TempDataDictionary
            {
                { "retainMe", "retainMeValue" },
                { "removeMe", "removeMeValue" },
                { "previouslyRemoved", "previouslyRemovedValue" }
            };
            object dummy = tempData["previouslyRemoved"]; // mark value for removal

            ControllerContext controllerContext = GetControllerContext(tempData);

            TempDataValueProviderFactory factory = new TempDataValueProviderFactory();

            // Act
            IValueProvider valueProvider = factory.GetValueProvider(controllerContext);
            ValueProviderResult nonExistentResult = valueProvider.GetValue("nonExistent");
            ValueProviderResult removeMeResult = valueProvider.GetValue("removeme");

            // Assert
            Assert.Null(nonExistentResult);
            Assert.Equal("removeMeValue", removeMeResult.AttemptedValue);
            Assert.Equal(CultureInfo.InvariantCulture, removeMeResult.Culture);

            // Verify that keys have been removed or retained correctly by the provider
            Mock<ITempDataProvider> mockTempDataProvider = new Mock<ITempDataProvider>();
            string[] retainedKeys = null;
            mockTempDataProvider
                .Setup(o => o.SaveTempData(controllerContext, It.IsAny<IDictionary<string, object>>()))
                .Callback(
                    delegate(ControllerContext cc, IDictionary<string, object> d) { retainedKeys = d.Keys.ToArray(); });

            tempData.Save(controllerContext, mockTempDataProvider.Object);
            Assert.Equal(expectedRetainedKeys, retainedKeys);
        }