Issue #985: Adding Functional Tests for ViewComponent Activators.

This commit is contained in:
sornaks 2014-09-30 09:03:55 -07:00
parent 6a886d39ab
commit 3d5f4a2bfd
10 changed files with 171 additions and 2 deletions

View File

@ -65,6 +65,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
[Fact]
public async Task ViewActivator_ActivatesDefaultInjectedProperties()
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = server.CreateClient();
var expected = @"<label for=""Hello"">Hello</label> world! /View/ConsumeServicesFromBaseType";
@ -79,6 +80,7 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
[Fact]
public async Task ViewActivator_ActivatesAndContextualizesInjectedServices()
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = server.CreateClient();
var expected = "4 test-value";
@ -93,10 +95,10 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
[Fact]
public async Task ViewActivator_ActivatesServicesFromBaseType()
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = server.CreateClient();
var expected =
@"/content/scripts/test.js";
var expected = @"/content/scripts/test.js";
// Act
var body = await client.GetStringAsync("http://localhost/View/ConsumeServicesFromBaseType");
@ -104,5 +106,65 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
// Assert
Assert.Equal(expected, body.Trim());
}
[Fact]
public async Task ViewComponentActivator_ActivatesProperties()
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = server.CreateClient();
var expected = @"Random Number:4";
// Act
var body = await client.GetStringAsync("http://localhost/View/ConsumeViewComponent");
// Assert
Assert.Equal(expected, body.Trim());
}
[Fact]
public async Task ViewComponentActivator_ActivatesPropertiesAndContextualizesThem()
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = server.CreateClient();
var expected = "test-value";
// Act
var body = await client.GetStringAsync("http://localhost/View/ConsumeValueComponent?test=test-value");
// Assert
Assert.Equal(expected, body.Trim());
}
[Fact]
public async Task ViewComponentActivator_ActivatesPropertiesAndContextualizesThem_WhenMultiplePropertiesArePresent()
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = server.CreateClient();
var expected = "Random Number:4 test-value";
// Act
var body = await client.GetStringAsync("http://localhost/View/ConsumeViewAndValueComponent?test=test-value");
// Assert
Assert.Equal(expected, body.Trim());
}
[Fact]
public async Task ViewComponentThatCannotBeActivated_ThrowsWhenAttemptedToBeInvoked()
{
// Arrange
var server = TestServer.Create(_provider, _app);
var client = server.CreateClient();
var expectedMessage = "TODO: No service for type 'ActivatorWebSite.CannotBeActivatedComponent+FakeType' " +
"has been registered.";
// Act & Assert
var ex = await Assert.ThrowsAsync<Exception>(
() => client.GetAsync("http://localhost/View/ConsumeCannotBeActivatedComponent"));
Assert.Equal(expectedMessage, ex.Message);
}
}
}

View File

@ -0,0 +1,23 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Mvc;
namespace ActivatorWebSite
{
[ViewComponent(Name = "CannotBeActivated")]
public class CannotBeActivatedComponent : ViewComponent
{
[Activate]
private FakeType Service { get; set; }
public IViewComponentResult Invoke()
{
return Content("Test");
}
private sealed class FakeType
{
}
}
}

View File

@ -0,0 +1,19 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Mvc;
namespace ActivatorWebSite
{
[ViewComponent(Name = "Number")]
public class NumberComponent : ViewComponent
{
[Activate]
public MyService MyTestService { get; set; }
public IViewComponentResult Invoke(string content)
{
return Content(content + ":" + MyTestService.Random);
}
}
}

View File

@ -0,0 +1,19 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Mvc;
namespace ActivatorWebSite
{
[ViewComponent(Name ="Value")]
public class ValueComponent : ViewComponent
{
[Activate]
public ViewService MyViewService { get; set; }
public IViewComponentResult Invoke()
{
return Content(MyViewService.GetValue());
}
}
}

View File

@ -0,0 +1,22 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNet.Mvc;
namespace ActivatorWebSite
{
[ViewComponent(Name = "ViewAndValue")]
public class ViewAndValueComponent : ViewComponent
{
[Activate]
public MyService MySampleService { get; set; }
[Activate]
public ViewService MyViewService { get; set; }
public IViewComponentResult Invoke(string content)
{
return Content(content + ":" + MySampleService.Random + " " + MyViewService.GetValue());
}
}
}

View File

@ -24,5 +24,25 @@ namespace ActivatorWebSite
{
return View();
}
public ViewResult ConsumeViewComponent()
{
return View();
}
public ViewResult ConsumeValueComponent()
{
return View();
}
public ViewResult ConsumeViewAndValueComponent()
{
return View();
}
public ViewResult ConsumeCannotBeActivatedComponent()
{
return View();
}
}
}

View File

@ -0,0 +1 @@
@Component.Invoke("CannotBeActivated")

View File

@ -0,0 +1 @@
@Component.Invoke("Value")

View File

@ -0,0 +1 @@
@Component.Invoke("ViewAndValue", "Random Number")

View File

@ -0,0 +1 @@
@Component.Invoke("Number", "Random Number")