简体   繁体   English

C#单元测试。 验证清单数

[英]C# Unit testing. Verify list count

I wanted to have a test case where I can verify a list of IRule count increases as new IRule item added. 我想要一个测试用例,可以验证随着新增的IRule项,IRule计数列表增加。 The method I am testing is AddRule. 我正在测试的方法是AddRule。 I wanted to keep the 'rules' property private. 我想让“规则”财产不公开。

Here's the code 这是代码

public class DefaultRulesManager : IRulesManager
{
    private List<IRule> rules;

    public DefaultRulesManager()
    {
        rules = new List<IRule>();
    }

    public void AddRule(IRule rule)
    {
        if (rule == null)
        {
            throw new ArgumentNullException("rule must be set");
        }

        rules.Add(rule);
    }

    public bool HasPassed<T, SKU>(T obj, SKU s)
    {
        IProduct product = (IProduct)obj;

        return rules.All(x => x.HasPassed(product, s));
    }
}

You could expose a Count read only property that returns the count of rules : 您可以公开一个Count只读属性,该属性返回rules的计数:

public int RulesCount 
{
    get { return rules.Count; }
}

There are various options here: 这里有多种选择:

  • Expose rules publicly but safely, eg via a ReadOnlyCollection<T> wrapper or as an IEnumerable<IRule> via return rules.Select(r => r); 公开但安全地公开rules ,例如通过ReadOnlyCollection<T>包装器或通过return rules.Select(r => r);作为IEnumerable<IRule> return rules.Select(r => r); to avoid the actual list being exposed via casting. 以避免实际列表通过强制公开显示。 This is reasonable if you have no objection to callers knowing what rules are in a manager, so long as they can't modify the list except via AddRule . 如果您不反对调用方知道管理器中的规则,只要他们不能通过AddRule修改列表,这是合理的。
  • Expose rules internally (ideally via a property - I wouldn't suggest an internal field; I'd also make the field readonly), and use InternalsVisibleTo to allow your test access to that. 在内部公开rules (最好是通过属性公开-我不建议使用内部字段;我还将字段设置为只读),并使用InternalsVisibleTo允许您的测试访问该字段。 (You could expose just the count if you want, but I'm not sure it's particularly beneficial.) (如果需要,您可以只公开计数,但是我不确定这是否特别有益。)
  • Stick to testing the public API - you can check whether all the rules you add into the list (in your test) are then consulted. 坚持测试公共API-您可以检查是否会参考添加到列表中(在测试中)的所有规则。

Personally I'd probably go for the former option - but some people take "only test the public API" as a golden rule never to be broken. 就我个人而言,我可能会选择前一种方法-但有些人将“仅测试公共API”作为永不中断的黄金法则。

Yet another idea, on the top of previous answers is to perform your unit tests against a class which inherits DefaultRulesManager . 在先前答案的顶部,还有一个想法是对继承DefaultRulesManager的类执行单元测试。

You'd have to change DefaultRulesManager class slightly by making rules member protected . 您必须通过使rules成员protected稍微更改DefaultRulesManager类。 Inherited class can be named DefaultRulesManagerTestable and provide public readonly property RulesCount which would return rules.Count . 继承的类可以命名为DefaultRulesManagerTestable并提供公共只读属性RulesCount ,该属性将返回rules.Count

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM