Use reflection or property in unit testing?

This is the class I'm a little worried about. My goal is a unit test address list:

public class LabelPrinter
{
    private readonly IEnumerable<Address> _addresses;

    public LabelPrinter(IEnumerable<Address> addresses)
    {
        _addresses = addresses;
    }

    public Document Create()
    {
        // ... Generate PDF, etc ...
    }
}

So what is better:

  • Use reflection to verify private property or
  • Since the original IEnumerable can be changed externally, make a public getter and test it instead?
+4
source share
4 answers

, , , , - . , , , , .

, . , , . , , , - .

- Address, , LabelPrinter. , , .

+7

addresses? , , , . , , , :

[Test]
public void Test()
{
    IEnumerable<Address> addresses = new List<Address>();
    LabelPrinter printer = new LabelPrinter(addresses);

    ... // execute your test here

    Assert.AreEqual(blah, addresses.get(0));
    // or any other assertion you want to make on the addresses list
    // created up above.
}
+1

, . 2.

0

A test Create, not a setter (which is effective because you are here). I believe that testing setters / getters will be a bit of a waste of time. Especially since most of the time, the setter must be performed to perform some other test. They are also for the most part too simple to fail.

Therefore, instead of confirming that it LabelPrinterhas _addresses, and this is Y, check that the output Createcontains the relevant data.

0
source

All Articles