vendredi 16 juin 2017

How to test "ApplyTo.Post" RuleSet in ServiceStack

I want to test my validator for post behavior described below.

public interface ITestValidator
{
    bool IsExists(string testName);
}

public class TestValidator : ITestValidator
{

    public bool IsExists(string testName)
    {
        return true;
    }
}

public class TestRequest
{
    public string Name { get; set; }
    public int? Level { get; set; }
}

public class TestRequestValidator : AbstractValidator<TestRequest>
{
    public ITestValidator Validator { get; set; }
    public TestRequestValidator()
    {
        RuleFor(p => p.Level).Must(p => p.HasValue && p > 0);
        RuleSet(ApplyTo.Post, () =>
        {
            RuleFor(p => p.Name).Must(p => !Validator.IsExists(p));
        });
    }
}

I created a test with xunit as below.

[Fact]
public void test_should_not_be_valid()
{
    var validator = new TestRequestValidator();
    var validationResult = validator.Validate(new TestRequest
    {
        Level = 1,
        Name = null
    });

    Assert.False(validationResult.IsValid);
}

This test is successed for

RuleFor(p => p.Level).Must(p => p.HasValue && p > 0);

But it is not working for

RuleFor(p => p.Name).Must(p => !Validator.IsExists(p));

How can i test validators for multiple operations like "ApplyTo.Post, ApplyTo.Get, ApplyTo.Delete"?

Aucun commentaire:

Enregistrer un commentaire