How to Invoke validate with ruleset specified without using the ValidateAndThrow method?

Hi,

I apologize if this is documented somewhere and I missed it. Is there a way to call the Validate method of a validator and specify the ruleSet to apply using the ApplyTo enum?

I’m looking for something like this:

[Test]
public void MerchantStatusValidator_ValidMid_IsValid()
{
        var testCase = new MerchantStatus()
        {
            MID = "12345678901234"
        };

        var result = this._sharedSut.Validate(testCase, ApplyTo.Get);

        Assert.IsTrue(result.IsValid);
    }

Thanks!

ServiceStack’s Request DTO Validators are typically meant to be executed in the context of a request so you may need to test them within an integration test, but you can apply the HTTP Method ruleSet with:

var validationResult = validator.Validate(
    new ValidationContext(requestDto, null, new MultiRuleSetValidatorSelector(HttpMethods.Get)) {
        Request = req
    });

Thanks this is what I was looking for - While I understand that these can be tested with an integration test, I’d rather use a unit test :slight_smile: We’ve already had some unexpected issues because the validators were missing test cases because regex expressions were wrong.

ok no worries, but a lot of ServiceStack functionality assumes there’s an AppHost is available, but in most cases you can just use an In Memory AppHost, e.g:

[Test]
public void My_unit_test()
{
    using (new BasicAppHost().Init())
    {
        //test ServiceStack classes
    }
}

Of if you prefer you can set it up once per test fixture with something like:

public class MyUnitTests
{
    ServiceStackHost appHost;
    public MyUnitTests() => appHost = new BasicAppHost().Init();

    [OneTimeTearDown]
    public void OneTimeTearDown() => appHost.Dispose();

    [Test]
    public void My_unit_test()
    {
        //test ServiceStack classes
   }
}