3

There is an ongoing debate about the following property, and if it should be tested or not. We're working in C#.

MyClass prop => value;

Should I be writing the following unit test :

IMyClass _viewModel = new MyClass();
Mock value = new Mock<Bar>();
Assert.AreEqual(value, _viewModel.Prop);

Personally, I say YES, because that property is still public, visible in the interface IMyClass, and is a simple requirement, like many others. Because of this, its initial state should be tested. In this case, that's its only state, but that is irrelevant.

Also, the object might change and become

MyClass prop => value + othervalue;

And slowly becomes more intelligent. I believe you need to be future proof, and more importantly, simply being 100% certain of the initial state of your objects.

Now, on the other hand, the opposite opinion goes as follows :

You're over testing, what you're essentially testing is if (true == true), because that property is not going to change, and it makes the test useless.

We can't find an agreement, does anyone have some knowledge or wisdom to share?

Also, I must be fully fair, we're talking about testing Colors in this very specific case, that means checking that _viewmodel.backgroundcoloris indeed _theme.defaultBackgroundColor, or, like they like to say white == white, and like I say whiteTodayButMaybeRedOrGreenTomorrow == white.

marked as duplicate by gnat, Greg Burghardt, Bart van Ingen Schenau, candied_orange, user22815 Mar 15 '17 at 21:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Well, not gonna find that answer with the keywords i was using ! – Gil Sand Mar 14 '17 at 9:41
  • the only time I would bother would be when I need to hit 100% coverage for saftey reasons and the property isn't already covered by more useful tests – jk. Mar 14 '17 at 11:35
1

I would NOT test simple properties. If it's a possibility some "logic" would be added to it, I might consider it, but then again you have to consider is it a possibility or not. Some other method that depends on this properties value (common case in for example view models) should have some validation logic that should be tested, and that test would start failing if the properties logic changes.

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