Updating test projects to use Microsoft.AspNet.Testing

This commit is contained in:
Pranav K 2014-03-25 15:10:31 -07:00
parent 12632c5fc3
commit af7d61113b
19 changed files with 12 additions and 388 deletions

View File

@ -51,8 +51,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Common.k10", "src\Common\Co
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.Mvc.Razor.Test.net45", "test\Microsoft.AspNet.Mvc.Razor.Test\Microsoft.AspNet.Mvc.Razor.Test.net45.csproj", "{3EB2CFF9-6E67-4C03-9AC4-2DD169024938}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestCommon.net45", "test\TestCommon\TestCommon.net45.csproj", "{75A07B53-C5EE-4995-A55B-27562C23BCCD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AspNet.Mvc.Rendering.Test.net45", "test\Microsoft.AspNet.Mvc.Rendering.Test\Microsoft.AspNet.Mvc.Rendering.Test.net45.csproj", "{68FC3791-A9E4-4EDE-93A5-C7AC7DC0ED6E}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "k10", "k10", "{A4E25DA1-9BC1-4534-BDAA-4AEB7C089B0F}"
@ -141,10 +139,6 @@ Global
{3EB2CFF9-6E67-4C03-9AC4-2DD169024938}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EB2CFF9-6E67-4C03-9AC4-2DD169024938}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EB2CFF9-6E67-4C03-9AC4-2DD169024938}.Release|Any CPU.Build.0 = Release|Any CPU
{75A07B53-C5EE-4995-A55B-27562C23BCCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{75A07B53-C5EE-4995-A55B-27562C23BCCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{75A07B53-C5EE-4995-A55B-27562C23BCCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75A07B53-C5EE-4995-A55B-27562C23BCCD}.Release|Any CPU.Build.0 = Release|Any CPU
{68FC3791-A9E4-4EDE-93A5-C7AC7DC0ED6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{68FC3791-A9E4-4EDE-93A5-C7AC7DC0ED6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68FC3791-A9E4-4EDE-93A5-C7AC7DC0ED6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
@ -187,7 +181,6 @@ Global
{998C5A2E-D043-465F-BE19-076D27444289} = {49EBEEDD-E117-4B91-B4BA-56FB80AF4F3C}
{3EB2CFF9-6E67-4C03-9AC4-2DD169024938} = {49EBEEDD-E117-4B91-B4BA-56FB80AF4F3C}
{68FC3791-A9E4-4EDE-93A5-C7AC7DC0ED6E} = {49EBEEDD-E117-4B91-B4BA-56FB80AF4F3C}
{75A07B53-C5EE-4995-A55B-27562C23BCCD} = {49EBEEDD-E117-4B91-B4BA-56FB80AF4F3C}
{5A219830-3C19-475D-901F-E580BA87DFF8} = {A4E25DA1-9BC1-4534-BDAA-4AEB7C089B0F}
EndGlobalSection
EndGlobal

View File

@ -3,6 +3,7 @@
"dependencies": {
"Microsoft.AspNet.Mvc.Core" : "",
"Microsoft.AspNet.Mvc" : "",
"Microsoft.AspNet.Testing": "0.1-alpha-*",
"Moq": "4.2.1312.1622",
"Xunit.KRunner": "0.1-alpha-*",
"xunit.abstractions": "2.0.0-aspnet-*",

View File

@ -1,4 +1,5 @@
using System;
using Microsoft.AspNet.Testing;
using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding.Test

View File

@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.AspNet.Testing;
using Moq;
using Xunit;

View File

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Microsoft.AspNet.Testing;
using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding.Test.Binders
@ -72,18 +73,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test.Binders
// GetMetadataForProperty
//[Fact]
//public void GetMetadataForPropertyNullContainerTypeThrows()
//{
// // Arrange
// TestableAssociatedMetadataProvider provider = new TestableAssociatedMetadataProvider();
// // Act & Assert
// ExceptionAssert.ThrowsArgumentNull(
// () => provider.GetMetadataForProperty(modelAccessor: null, containerType: null, propertyName: "propertyName"),
// "containerType");
//}
[Fact]
public void GetMetadataForPropertyNullOrEmptyPropertyNameThrows()
{

View File

@ -12,19 +12,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
private readonly DataAnnotationsModelMetadataProvider _metadataProvider = new DataAnnotationsModelMetadataProvider();
[Fact]
public void GetValidatorsThrowsIfMetadataIsNull()
{
// Arrange
var metadata = _metadataProvider.GetMetadataForType(null, typeof(object));
var provider = new Mock<AssociatedValidatorProvider> { CallBase = true };
// Act & Assert
ExceptionAssert.ThrowsArgumentNull(
() => provider.Object.GetValidators(metadata: null),
"metadata");
}
[Fact]
public void GetValidatorsForPropertyWithLocalAttributes()
{

View File

@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.AspNet.Testing;
#if NET45
using Moq;
using Moq.Protected;
@ -13,19 +14,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding
{
private static DataAnnotationsModelMetadataProvider _metadataProvider = new DataAnnotationsModelMetadataProvider();
[Fact]
public void ConstructorGuards()
{
// Arrange
var metadata = _metadataProvider.GetMetadataForType(null, typeof(object));
var attribute = new RequiredAttribute();
// Act & Assert
ExceptionAssert.ThrowsArgumentNull(
() => new DataAnnotationsModelValidator(null),
"attribute");
}
[Fact]
public void ValuesSet()
{

View File

@ -1,4 +1,5 @@
using System;
using Microsoft.AspNet.Testing;
using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding

View File

@ -2,6 +2,7 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Runtime.Serialization;
using Microsoft.AspNet.Testing;
using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding

View File

@ -1,7 +1,7 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using Microsoft.AspNet.Mvc.ModelBinding.Test;
using Microsoft.AspNet.Testing;
using Xunit;
namespace Microsoft.AspNet.Mvc.ModelBinding

View File

@ -19,18 +19,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
});
[Fact]
public void ContainsPrefix_GuardClauses()
{
// Arrange
var valueProvider = new ReadableStringCollectionValueProvider(_backingStore, null);
// Act & assert
ExceptionAssert.ThrowsArgumentNull(
() => valueProvider.ContainsPrefix(null),
"prefix");
}
[Fact]
public void ContainsPrefix_WithEmptyCollection_ReturnsFalseForEmptyPrefix()
{
@ -126,18 +114,6 @@ namespace Microsoft.AspNet.Mvc.ModelBinding.Test
Assert.Equal("bar.baz", kvp.Value);
}
[Fact]
public void GetValue_GuardClauses()
{
// Arrange
var valueProvider = new ReadableStringCollectionValueProvider(_backingStore, null);
// Act & assert
ExceptionAssert.ThrowsArgumentNull(
() => valueProvider.GetValue(null),
"key");
}
[Fact]
public void GetValue_SingleValue()
{

View File

@ -3,6 +3,7 @@
"dependencies": {
"Microsoft.AspNet.Abstractions": "0.1-alpha-*",
"Microsoft.AspNet.PipelineCore": "0.1-alpha-*",
"Microsoft.AspNet.Testing": "0.1-alpha-*",
"Microsoft.ComponentModel.DataAnnotations" : "4.0.10.0",
"Microsoft.AspNet.Mvc.ModelBinding" : "",
"Newtonsoft.Json": "5.0.8",

View File

@ -2,8 +2,8 @@
"version" : "0.1-alpha-*",
"dependencies": {
"Microsoft.AspNet.Razor" : "0.1-alpha-*",
"Microsoft.AspNet.Testing" : "0.1-alpha-*",
"Microsoft.AspNet.Mvc.Razor.Host" : "",
"TestCommon" : "",
"Xunit.KRunner": "0.1-alpha-*",
"xunit.abstractions": "2.0.0-aspnet-*",
"xunit.assert": "2.0.0-aspnet-*",

View File

@ -5,7 +5,7 @@
"Microsoft.AspNet.Razor": "0.1-alpha-*",
"Microsoft.AspNet.Mvc.Razor" : "",
"Microsoft.AspNet.Mvc.Razor.Host" : "",
"TestCommon" : "",
"Microsoft.AspNet.Testing" : "0.1-alpha-*",
"Xunit.KRunner": "0.1-alpha-*",
"xunit.abstractions": "2.0.0-aspnet-*",
"xunit.assert": "2.0.0-aspnet-*",

View File

@ -5,7 +5,7 @@
"Microsoft.AspNet.PipelineCore": "0.1-alpha-*",
"Microsoft.AspNet.Mvc.ModelBinding" : "",
"Microsoft.AspNet.Mvc.Rendering" : "",
"TestCommon" : "",
"Microsoft.AspNet.Testing" : "0.1-alpha-*",
"Xunit.KRunner": "0.1-alpha-*",
"xunit.abstractions": "2.0.0-aspnet-*",
"xunit.assert": "2.0.0-aspnet-*",

View File

@ -1,72 +0,0 @@
using System;
using System.Globalization;
using System.Threading;
using Xunit;
namespace Microsoft.AspNet.Mvc
{
internal class CultureReplacer : IDisposable
{
private const string _defaultCultureName = "en-GB";
private const string _defaultUICultureName = "en-US";
private static readonly CultureInfo _defaultCulture = new CultureInfo(_defaultCultureName);
private readonly CultureInfo _originalCulture;
private readonly CultureInfo _originalUICulture;
private readonly long _threadId;
// Culture => Formatting of dates/times/money/etc, defaults to en-GB because en-US is the same as InvariantCulture
// We want to be able to find issues where the InvariantCulture is used, but a specific culture should be.
//
// UICulture => Language
public CultureReplacer(string culture = _defaultCultureName, string uiCulture = _defaultUICultureName)
{
_originalCulture = CultureInfo.DefaultThreadCurrentCulture;
_originalUICulture = CultureInfo.DefaultThreadCurrentUICulture;
_threadId = Thread.CurrentThread.ManagedThreadId;
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo(culture);
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo(uiCulture);
}
/// <summary>
/// The name of the culture that is used as the default value for Thread.CurrentCulture when CultureReplacer is used.
/// </summary>
public static string DefaultCultureName
{
get { return _defaultCultureName; }
}
/// <summary>
/// The name of the culture that is used as the default value for Thread.UICurrentCulture when CultureReplacer is used.
/// </summary>
public static string DefaultUICultureName
{
get { return _defaultUICultureName; }
}
/// <summary>
/// The culture that is used as the default value for Thread.CurrentCulture when CultureReplacer is used.
/// </summary>
public static CultureInfo DefaultCulture
{
get { return _defaultCulture; }
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
Assert.True(Thread.CurrentThread.ManagedThreadId == _threadId,
"The current thread is not the same as the thread invoking the constructor. This should never happen.");
CultureInfo.DefaultThreadCurrentCulture = _originalCulture;
CultureInfo.DefaultThreadCurrentUICulture = _originalUICulture;
}
}
}
}

View File

@ -1,170 +0,0 @@
using System;
using System.Reflection;
using Xunit;
namespace Microsoft.AspNet.Mvc
{
internal static class ExceptionAssert
{
/// <summary>
/// Verifies that an exception of the given type (or optionally a derived type) is thrown.
/// </summary>
/// <typeparam name="TException">The type of the exception expected to be thrown</typeparam>
/// <param name="testCode">A delegate to the code to be tested</param>
/// <returns>The exception that was thrown, when successful</returns>
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
public static TException Throws<TException>(Action testCode)
where TException : Exception
{
Type exceptionType = typeof(TException);
Exception exception = RecordException(testCode);
TargetInvocationException tie = exception as TargetInvocationException;
if (tie != null)
{
exception = tie.InnerException;
}
Assert.NotNull(exception);
return Assert.IsAssignableFrom<TException>(exception);
}
/// <summary>
/// Verifies that an exception of the given type (or optionally a derived type) is thrown.
/// Also verifies that the exception message matches.
/// </summary>
/// <typeparam name="TException">The type of the exception expected to be thrown</typeparam>
/// <param name="testCode">A delegate to the code to be tested</param>
/// <param name="exceptionMessage">The exception message to verify</param>
/// <returns>The exception that was thrown, when successful</returns>
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
public static TException Throws<TException>(Action testCode, string exceptionMessage)
where TException : Exception
{
var ex = Throws<TException>(testCode);
VerifyExceptionMessage(ex, exceptionMessage);
return ex;
}
/// <summary>
/// Verifies that an exception of the given type (or optionally a derived type) is thrown.
/// Also verified that the exception message matches.
/// </summary>
/// <typeparam name="TException">The type of the exception expected to be thrown</typeparam>
/// <param name="testCode">A delegate to the code to be tested</param>
/// <param name="exceptionMessage">The exception message to verify</param>
/// <returns>The exception that was thrown, when successful</returns>
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
public static TException Throws<TException>(Func<object> testCode, string exceptionMessage)
where TException : Exception
{
return Throws<TException>(() => { testCode(); }, exceptionMessage);
}
/// <summary>
/// Verifies that the code throws an <see cref="ArgumentException"/> (or optionally any exception which derives from it).
/// </summary>
/// <param name="testCode">A delegate to the code to be tested</param>
/// <param name="paramName">The name of the parameter that should throw the exception</param>
/// <param name="exceptionMessage">The exception message to verify</param>
/// <returns>The exception that was thrown, when successful</returns>
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
public static ArgumentException ThrowsArgument(Action testCode, string paramName, string exceptionMessage)
{
var ex = Throws<ArgumentException>(testCode);
if (paramName != null)
{
Assert.Equal(paramName, ex.ParamName);
}
VerifyExceptionMessage(ex, exceptionMessage, partialMatch: true);
return ex;
}
/// <summary>
/// Verifies that the code throws an ArgumentNullException (or optionally any exception which derives from it).
/// </summary>
/// <param name="testCode">A delegate to the code to be tested</param>
/// <param name="paramName">The name of the parameter that should throw the exception</param>
/// <returns>The exception that was thrown, when successful</returns>
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
public static ArgumentNullException ThrowsArgumentNull(Action testCode, string paramName)
{
var ex = Throws<ArgumentNullException>(testCode);
if (paramName != null)
{
Assert.Equal(paramName, ex.ParamName);
}
return ex;
}
/// <summary>
/// Verifies that the code throws an ArgumentNullException with the expected message that indicates that the value cannot
/// be null or empty.
/// </summary>
/// <param name="testCode">A delegate to the code to be tested</param>
/// <param name="paramName">The name of the parameter that should throw the exception</param>
/// <returns>The exception that was thrown, when successful</returns>
/// <exception cref="ThrowsException">Thrown when an exception was not thrown, or when an exception of the incorrect type is thrown</exception>
public static ArgumentException ThrowsArgumentNullOrEmpty(Action testCode, string paramName)
{
return Throws<ArgumentException>(testCode, "Value cannot be null or empty.\r\nParameter name: " + paramName);
}
// We've re-implemented all the xUnit.net Throws code so that we can get this
// updated implementation of RecordException which silently unwraps any instances
// of AggregateException. In addition to unwrapping exceptions, this method ensures
// that tests are executed in with a known set of Culture and UICulture. This prevents
// tests from failing when executed on a non-English machine.
private static Exception RecordException(Action testCode)
{
try
{
using (new CultureReplacer())
{
testCode();
}
return null;
}
catch (Exception exception)
{
return UnwrapException(exception);
}
}
private static Exception UnwrapException(Exception exception)
{
AggregateException aggEx = exception as AggregateException;
if (aggEx != null)
{
return aggEx.GetBaseException();
}
return exception;
}
private static void VerifyException(Type exceptionType, Exception exception)
{
Assert.NotNull(exception);
Assert.IsAssignableFrom(exceptionType, exception);
}
private static void VerifyExceptionMessage(Exception exception, string expectedMessage, bool partialMatch = false)
{
if (expectedMessage != null)
{
if (!partialMatch)
{
Assert.Equal(expectedMessage, exception.Message);
}
else
{
Assert.Contains(expectedMessage, exception.Message);
}
}
}
}
}

View File

@ -1,56 +0,0 @@
using System;
using System.Globalization;
using System.Reflection;
using Xunit.Sdk;
namespace Microsoft.AspNet.Mvc
{
/// <summary>
/// Replaces the current culture and UI culture for the test.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
internal class ReplaceCultureAttribute : BeforeAfterTestAttribute
{
private const string _defaultCultureName = "en-GB";
private const string _defaultUICultureName = "en-US";
private static readonly CultureInfo _defaultCulture = new CultureInfo(_defaultCultureName);
private CultureInfo _originalCulture;
private CultureInfo _originalUICulture;
public ReplaceCultureAttribute()
{
Culture = _defaultCulture;
UICulture = _defaultCulture;
}
/// <summary>
/// Sets <see cref="Thread.CurrentCulture"/> for the test. Defaults to en-GB.
/// </summary>
/// <remarks>
/// en-GB is used here as the default because en-US is equivalent to the InvariantCulture. We
/// want to be able to find bugs where we're accidentally relying on the Invariant instead of the
/// user's culture.
/// </remarks>
public CultureInfo Culture { get; set; }
/// <summary>
/// Sets <see cref="Thread.CurrentUICulture"/> for the test. Defaults to en-US.
/// </summary>
public CultureInfo UICulture { get; set; }
public override void Before(MethodInfo methodUnderTest)
{
_originalCulture = CultureInfo.DefaultThreadCurrentCulture;
_originalUICulture = CultureInfo.DefaultThreadCurrentUICulture;
CultureInfo.DefaultThreadCurrentCulture = Culture;
CultureInfo.DefaultThreadCurrentUICulture = UICulture;
}
public override void After(MethodInfo methodUnderTest)
{
CultureInfo.DefaultThreadCurrentCulture = _originalCulture;
CultureInfo.DefaultThreadCurrentUICulture = _originalUICulture;
}
}
}

View File

@ -1,17 +0,0 @@
{
"shared": "*.cs",
"dependencies": {
"xunit.abstractions": "2.0.0-aspnet-*",
"xunit.assert": "2.0.0-aspnet-*",
"xunit.core": "2.0.0-aspnet-*",
"xunit.execution": "2.0.0-aspnet-*"
},
"configurations": {
"net45": {
"dependencies": {
"Moq": "4.2.1312.1622",
"System.Reflection": ""
}
}
}
}