Ejemplo n.º 1
0
        public void Execute_FallsbackToServices_WhenNoJsonFormatterIsProvided()
        {
            // Arrange
            var view = Mock.Of <IView>();

            var serviceProvider = new Mock <IServiceProvider>();

            serviceProvider
            .Setup(p => p.GetService(typeof(JsonOutputFormatter)))
            .Returns(new JsonOutputFormatter())
            .Verifiable();

            var buffer = new MemoryStream();

            var result = new JsonViewComponentResult(1);
            var viewComponentContext = GetViewComponentContext(view, buffer);

            viewComponentContext.ViewContext.HttpContext.RequestServices = serviceProvider.Object;

            // Act
            result.Execute(viewComponentContext);
            buffer.Position = 0;

            // Assert
            Assert.Equal("1", new StreamReader(buffer).ReadToEnd());
            serviceProvider.Verify();
        }
Ejemplo n.º 2
0
        public void Execute_UsesSerializerSettingsFromOptions_IfNotProvided()
        {
            // Arrange
            var view   = Mock.Of <IView>();
            var buffer = new MemoryStream();
            var viewComponentContext = GetViewComponentContext(view, buffer);

            var result = new JsonViewComponentResult(new { foo = "abcd" });

            viewComponentContext.ViewContext.HttpContext.Response.Body = buffer;

            // Act
            result.Execute(viewComponentContext);

            // Assert
            Assert.Equal("{\"foo\":\"abcd\"}", Encoding.UTF8.GetString(buffer.ToArray()));
        }
Ejemplo n.º 3
0
        public void Execute_SerializesData_UsingSpecifiedFormatter()
        {
            // Arrange
            var view   = Mock.Of <IView>();
            var buffer = new MemoryStream();
            var viewComponentContext = GetViewComponentContext(view, buffer);

            var expectedFormatter = new JsonOutputFormatter();
            var result            = new JsonViewComponentResult(1, expectedFormatter);

            // Act
            result.Execute(viewComponentContext);
            buffer.Position = 0;

            // Assert
            Assert.Equal(expectedFormatter, result.Formatter);
            Assert.Equal("1", new StreamReader(buffer).ReadToEnd());
        }
Ejemplo n.º 4
0
        public void Execute_UsesFormatter_WithSpecifiedSerializerSettings()
        {
            // Arrange
            var view   = Mock.Of <IView>();
            var buffer = new MemoryStream();
            var viewComponentContext = GetViewComponentContext(view, buffer);

            var serializerSettings = new JsonSerializerSettings();

            serializerSettings.Formatting = Formatting.Indented;

            var result = new JsonViewComponentResult("abc", serializerSettings);

            // Act
            result.Execute(viewComponentContext);

            // Assert
            Assert.Same(serializerSettings, result.Formatter.SerializerSettings);
        }
Ejemplo n.º 5
0
        public void Execute_Throws_IfNoFormatterCanBeResolved()
        {
            // Arrange
            var expected = "No service for type 'Microsoft.AspNet.Mvc.JsonOutputFormatter'" +
                           " has been registered.";

            var view = Mock.Of <IView>();

            var serviceProvider = new ServiceCollection().BuildServiceProvider();

            var buffer = new MemoryStream();

            var result = new JsonViewComponentResult(1);
            var viewComponentContext = GetViewComponentContext(view, buffer);

            viewComponentContext.ViewContext.HttpContext.RequestServices = serviceProvider;

            // Act
            var ex = Assert.Throws <InvalidOperationException>(() => result.Execute(viewComponentContext));

            // Assert
            Assert.Equal(expected, ex.Message);
        }
Ejemplo n.º 6
0
        public void Execute_UsesSerializer_WithSpecifiedSerializerSettings()
        {
            // Arrange
            var view   = Mock.Of <IView>();
            var buffer = new MemoryStream();
            var viewComponentContext = GetViewComponentContext(view, buffer);

            var serializerSettings = new JsonSerializerSettings();

            serializerSettings.Formatting = Formatting.Indented;

            var result = new JsonViewComponentResult(new { foo = "abcd" }, serializerSettings);

            viewComponentContext.ViewContext.HttpContext.Response.Body = buffer;

            // Act
            result.Execute(viewComponentContext);

            // Assert
            Assert.Equal(
                $"{{{Environment.NewLine}  \"foo\": \"abcd\"{Environment.NewLine}}}",
                Encoding.UTF8.GetString(buffer.ToArray()));
        }