8

Here's a silly psuedo-object to illustrate the point:

public class Testing
{
    public object MyUnderlyingObject = new object { Property = 44 };

    public MyPropertyWrapper
    {
        get
        {
            return MyUnderlyingObject.Property;
        }
        set
        {
            MyUnderyingObject.Property = value
        }
    }
}

When faced with situations such as this, I like to write tests that assert existing state, in order to illustrate that the value under test has actually changed. For example:

[TestMethod]
public void TestMyProperty()
{
    Testing tester = new Testing();                   // arrange
    Assert.AreEqual(44, MyUnderlyingObject.Property); // assert existing value
    tester.MyPropertyWrapper = 55;                    // act
    Assert.AreEqual(55, MyUnderlyingObject.Property); // assert value has changed
}

Is this just a waste of code and processing time?

3
  • 3
    If you want to test it and think it should be tested, it is not a waste of code.
    – Andy
    Feb 8, 2016 at 12:46
  • 1
    What is the purpose of the test? If you just want to check that get(set x) == x then it doesn't matter what the previous state was. If you want to check that some operation causes a change in the value then you probably want to assert some relationship between the before and after states. In that case you don't need to assert the initial value, just the change. If you do need to assert the initial state of the object then that should have its own test and you should avoid repeated assertions about it in other tests.
    – Lee
    Feb 8, 2016 at 12:58
  • I think there is some use for this. Maybe not in the case you describe, but in other cases. For example, if there is a complex setup process, but the resulting state is easy to describe, having an assertion that the system is in the proper state after setup may be the easiest to read. Feb 15, 2016 at 3:19

2 Answers 2

11

You are testing two things, so you would be better off with two tests:

[TestMethod]
public void TestingClass_CorrectlyInitialisesProperty()
{
    Testing tester = new Testing();                  
    Assert.AreEqual(44, MyUnderlyingObject.Property);
}

[TestMethod]
public void TestingClass_CorrectlyChangesPropertyViaSetter()
{
    Testing tester = new Testing();                  
    tester.MyPropertyWrapper = 55;                    

    Assert.AreEqual(55, MyUnderlyingObject.Property);
}
1
  • But what if MyUnderlyingObject.Property is not initialized at start? The value of 44 here is arbitrary - usually it would be null, and I don't think that your second method actually tests that there was a change in value, only that the value is 55, not that the PropertyWrapper actually changed it.
    – Startec
    Aug 18, 2021 at 23:04
3

This depends on the programming language. In a compiled language, your typo above

         MyUnderyingObject.Property = value;
                ^
                - missing "l" here

would have been caught by the compiler. In an interpreted language with no prior sytnax validation, this code may only be checked for syntactical correctness when it is executed. For this it makes perfect sense to have a unit test to find this bug.

(And yes, I am aware that this typo was probably not made intentional when you wrote the question. Please leave in the question as it is - it gives a good example for what such a unit test might be useful).

1
  • 1
    I think that should be "perfect" rather than "perfectly" for grammatical correctness :-) Feb 14, 2016 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.