BeniaminoBaggins
BeniaminoBaggins

Reputation: 12473

Convert a function with 2 parameters into a lambda action

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

Answers (1)

Felipe Cruz
Felipe Cruz

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

Related Questions