装饰器模式的单元测试

发布于 2024-09-05 06:51:32 字数 2708 浏览 4 评论 0原文

我最近使用装饰器模式解决了我的问题之一。一切工作正常,一切都足够解耦(或者我认为如此),我能够单独对每个有效字段进行单元测试。

我的问题是,NameValidator 和 AgeValidator 是否都通过了 Validate() 和 IsValid() (抽象)函数的测试。我是否还需要对我的 ValidationDecorator 类(尚未创建)进行单元测试? ValidationDecorator 将负责用每个验证类装饰我的验证器。

public abstract class FieldValidator
{
    protected IMessage validateReturnType;

    public FieldValidator() { }

    public bool IsValid()
    {
        return (validateReturnType.GetType() == typeof(Success));
    }
}

public class NameValidator : FieldValidator, IValidator
{
    private string name;

    public NameValidator(string _name) { 
        name = _name;
    }

    public IMessage Validate()
    {
        if (name.Length < 5)
        {
            validateReturnType = new Error("Name error.");
        }
        else
        {
            validateReturnType = new Success("Name no errror.");
        }

        return validateReturnType;
    }
}

public class AgeValidator : FieldValidator, IValidator
{
    private int age;

    public AgeValidator(int _age)
    {
        age = _age;
    }

    public IMessage Validate()
    {
        if (age <= 18)
        {
            validateReturnType = new Error("Age error.");
        }
        else
        {
            validateReturnType = new Success("Age no errror.");
        }

        return validateReturnType;
    }
}

public interface IValidator
{
    IMessage Validate();
    bool IsValid();
}

这是我的单元测试。

[TestFixture]
public class ValidatorTest
{
    Type successType;
    Type errorType;

    Model m;

    [SetUp]
    public void SetUp()
    {
        successType = typeof(Success);
        errorType = typeof(Error);

        m = new Model();
        m.Name = "Mike Cameron";
        m.Age = 19;
        m.Height = 325;

        Validator v = new Validator();
        v.Validate(m);
    }

    [Test]
    public void ValidateNameTest()
    {
        IValidator im = new NameValidator(m.Name);

        IMessage returnObj = im.Validate();

        Assert.AreEqual(successType, returnObj.GetType());
    }

    [Test]
    public void IsValidNameTest()
    {
        IValidator im = new NameValidator(m.Name);

        IMessage returnObj = im.Validate();

        Assert.IsTrue(im.IsValid());
    }

    [Test]
    public void ValidateAgeTest()
    {
        IValidator im = new AgeValidator(m.Age);

        IMessage returnObj = im.Validate();

        Assert.AreEqual(successType, returnObj.GetType(), "Must be over 18");
    }

    [Test]
    public void IsValidAgeTest()
    {
        IValidator im = new AgeValidator(m.Age);

        IMessage returnObj = im.Validate();

        Assert.IsTrue(im.IsValid());
    }

谢谢。

I recently solved one of my problems using the decorator pattern. Everything works fine and everything is decoupled enough (or so I think) that I am able to unit test each validitable field separately.

My question is, if the NameValidator and AgeValidator both pass the tests for the Validate() and IsValid() (abstract) functions. Do I still need to unit test my ValidationDecorator class (not created yet)? ValidationDecorator would be reponsible for decorating my validator with each validation class.

public abstract class FieldValidator
{
    protected IMessage validateReturnType;

    public FieldValidator() { }

    public bool IsValid()
    {
        return (validateReturnType.GetType() == typeof(Success));
    }
}

public class NameValidator : FieldValidator, IValidator
{
    private string name;

    public NameValidator(string _name) { 
        name = _name;
    }

    public IMessage Validate()
    {
        if (name.Length < 5)
        {
            validateReturnType = new Error("Name error.");
        }
        else
        {
            validateReturnType = new Success("Name no errror.");
        }

        return validateReturnType;
    }
}

public class AgeValidator : FieldValidator, IValidator
{
    private int age;

    public AgeValidator(int _age)
    {
        age = _age;
    }

    public IMessage Validate()
    {
        if (age <= 18)
        {
            validateReturnType = new Error("Age error.");
        }
        else
        {
            validateReturnType = new Success("Age no errror.");
        }

        return validateReturnType;
    }
}

public interface IValidator
{
    IMessage Validate();
    bool IsValid();
}

This is my unit test.

[TestFixture]
public class ValidatorTest
{
    Type successType;
    Type errorType;

    Model m;

    [SetUp]
    public void SetUp()
    {
        successType = typeof(Success);
        errorType = typeof(Error);

        m = new Model();
        m.Name = "Mike Cameron";
        m.Age = 19;
        m.Height = 325;

        Validator v = new Validator();
        v.Validate(m);
    }

    [Test]
    public void ValidateNameTest()
    {
        IValidator im = new NameValidator(m.Name);

        IMessage returnObj = im.Validate();

        Assert.AreEqual(successType, returnObj.GetType());
    }

    [Test]
    public void IsValidNameTest()
    {
        IValidator im = new NameValidator(m.Name);

        IMessage returnObj = im.Validate();

        Assert.IsTrue(im.IsValid());
    }

    [Test]
    public void ValidateAgeTest()
    {
        IValidator im = new AgeValidator(m.Age);

        IMessage returnObj = im.Validate();

        Assert.AreEqual(successType, returnObj.GetType(), "Must be over 18");
    }

    [Test]
    public void IsValidAgeTest()
    {
        IValidator im = new AgeValidator(m.Age);

        IMessage returnObj = im.Validate();

        Assert.IsTrue(im.IsValid());
    }

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

滴情不沾 2024-09-12 06:51:32

经验法则是“测试所有可能损坏的东西”。现在,在现实生活中判断什么可能会破坏绝非易事。需要练习和经验才能做到正确。而且不可能提供一般性建议,尤其是在没有看到您的设计和代码的情况下。

因此,最终,只有您可以可靠地评估您是否有足够的信心认为 ValidationDecorator 是如此微不足道,永远不会崩溃。如果有疑问,最好宁可进行太多测试:-) 这意味着您要在可能不重要的任务上花费一些额外的时间。相反 - 未能编写所需的单元测试 - 意味着您可能会让错误漏网,这通常是一个更大的问题。

The rule of thumb is "test everything which could possibly break". Now, judging what can possibly break is far from trivial under real life circumstances. It takes practice and experience to get it right. And it is impossible to give general advice, especially without seeing your design and code.

So in the end, only you can reliably assess whether or not you feel confident enough that ValidationDecorator is so trivial, it can never break. If in doubt, it is better to err on the side of too many tests :-) That means you spend some extra time on a possibly unimportant task. The opposite - failing to write a needed unit test - means you may let a bug slip through your net, which is typically a bigger issue.

心作怪 2024-09-12 06:51:32

是的,您应该对任何可能包含错误的类进行单元测试。要对 ValidationDecorator 类进行单元测试,您应该使用 IValidator 的模拟或存根实现。

Yes, you should unit test any class that could contain bugs. To unit test your ValidationDecorator class, you should use mock or stub implementations of IValidator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文