views:

49

answers:

1

I'm fairly new to unit testing and we are actually attempting to use it on a project. There is a property like this.

    public TimeSpan CountDown
    {
        get
        {
            return _countDown;
        }
        set
        {
            long fraction = value.Ticks % 10000000;
            value -= TimeSpan.FromTicks(fraction);
            if(fraction > 5000000)
                value += TimeSpan.FromSeconds(1);
            if(_countDown != value)
            {
                _countDown = value;
                NotifyChanged("CountDown");
            }
        }
    }

My test looks like this.

[TestMethod]
    public void CountDownTest_GetSet_PropChangedShouldFire()
    {
        ManualRafflePresenter target = new ManualRafflePresenter();
        bool fired = false;
        string name = null;
        target.PropertyChanged += new PropertyChangedEventHandler((o, a) =>
        {
            fired = true;
            name = a.PropertyName;
        });
        TimeSpan expected = new TimeSpan(0, 1, 25);
        TimeSpan actual;
        target.CountDown = expected;
        actual = target.CountDown;
        Assert.AreEqual(expected, actual);
        Assert.IsTrue(fired);
        Assert.AreEqual("CountDown", name);
    }

The question is how do I test the code in the setter? Do I break it out into a method? If I do it would probably be private since no one else needs to use this. But they say not to test private methods. Do make a class if this is the only case? would two uses of this code make a class worthwhile? What is wrong with this code from a design standpoint. What is correct?

+4  A: 

The way you've got is fine (call the setter and then check the get returns the expected value).

Make sure you choose a selection of test values that exercise all the paths in that setter. A single set/get test isn't sufficient coverage.

Paolo
That is what I thought I just could not think of how to make values what I needed. I believe I have figured it out. Thanks.
nportelli