Tuesday, June 26, 2007

I have been trying to get better at writing unit tests and feel I am succeeding.  I have been recently reading about how to write good unit tests and one thng is that they should be easy and relatively quick to write.  Therefore rather than using comments to describe the test I have been using the description as the method name (all be it more verbose).  For example a model test I have just written in the GroupTests Test Fixture is:

[Test]
public void ValidationWithDuplicateDeliveryMethods()
{
...
}

I find this removes the need for the summary as it says exactly what I am testing and it also provides a better descriptionof the test when I return to it with NUnit GUI. 

What do you guys think about this?

6/26/2007 4:26:39 PM (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [1]  |  Trackback

I have had occassion recently to have to set a class private variable in a class form a unit test.

[This was for settting _Id values in Model object which have no public setter]. 

this is how I did it:

Group testGroup = new Group();
Seller testSeller = new Seller();
Type t1 = testGroup.GetType();
FieldInfo fi = t1.GetField("_Id", BindingFlags.NonPublic | BindingFlags.Instance);

Type t2 = testSeller.GetType();
FieldInfo fi2 = t2.GetField("_Id", BindingFlags.NonPublic | BindingFlags.Instance);

fi.SetValue(testGroup, 20);
fi2.SetValue(testSeller, 20);

6/26/2007 4:16:28 PM (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  |  Trackback
 Monday, June 04, 2007

It is possible to create pagination using NHibernate query's:

IList<T> = session.CreateCriteria(typeof(T))
.AddOrder(Order.Asc("Column"))
.SetFirstResult(21)
.SetMaximumResults(20)
.List<T>();

It is also possible to return a single result using:

T item = session.CreateCriteria(typeof(T))
.Add(Expression.Eq("PropertyName", "PropertyValue"))
.UniqueResult();

I have implemented this unique result in the seller and group methods for getting the objects by urlname.  tyhis returns null if not found.

6/4/2007 10:27:14 AM (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  |  Trackback