Reputation: 12473
I want to test (with using Microsoft.VisualStudio.TestTools.UnitTesting
) that the top line of this test function causes a DataMisalignedException
to be thrown.
namespace xxx.Services.SupplierApiTests
{
[TestClass]
public class JsonSchemaValidatorTests
{
[TestMethod]
public void ShouldThrowOnBadPhoneNumber()
{
JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders(), "./provider-schema.json");
Action<IList, string> myAction = (x, y) => JsonSchemaValidator.validateAgainstJsonSchema(x, y);
Assert.ThrowsException<DataMisalignedException>(myAction);
}
}
}
How can I use JsonSchemaValidator.validateAgainstJsonSchema
as an action, passing in the two arguments from the top line of the test? My attempt is in the code above but doesn't pass the two parameters.
Upvotes: 0
Views: 70
Reputation: 1149
To indicate that an exception is expected during test method execution you can make use of the [ExpectedException]
attribute on top of the test method.
[TestClass]
public class JsonSchemaValidatorTests
{
[TestMethod]
[ExpectedException(typeof(DataMisalignedException))]
public void ShouldThrowOnBadPhoneNumber()
{
JsonSchemaValidator.validateAgainstJsonSchema(ProviderService.getErronousProviders(), "./provider-schema.json");
}
}
Upvotes: 1