views:

50

answers:

1

I have in the controller a custom DataView object that comprises 2 lists. I populate them and than pass the DataView object as model for my view. In view when displaying the data I am checking for null reference. I wonder how to write unit tests to ensure that the programmer did not forget to check for null reference in the view. I would like to test the view for the exceptions of this type. If somewone would give me a good sample with rhino mocks testing framework I would greatly apreciated, because I use it in mostly parts. Thanks.

+1  A: 

While I don't think the following is ideal, it will prevent NREs:

public class ViewModel
{
  public IEnumerable<Item> Items
  {
     get { return items ?? new List<Item>(); }
     set { items = value; }
  }
}
Chris Missal
Yes indeed you are right, but assume that I don't want to change Itmes declaration, it's interesting how to write tests on the view to ensure that the programmer checked for null reference.
diadiora
And this approach is not suitable for smth like:public Item TheItembecause in this case I want to take steps in dependance of null value or not, in other case I'll have to check for some field of the Item object for its value, and this is not very clean approach in my mind.
diadiora
+1 for ?? - I didnt know about!
Maciej