Test Expected Exception with NUnit 2.5

Testing the expected exceptions has been improved a lot by NUnit 2.5, let says you have a scenario that a method checks for the input parameters and throws some ArgumentNullException exceptions, it was kind of cumbersome to cover all those cases in your tests, normally, you will have multiple test methods using ExpectedExceptionAttribute. But with NUnit 2.5.x, we can write something likes


[Test]

public void AndSpecification_Construction_With_Null_Inputs_Test()

{

   // Arrange
   ISpecification<MockEntity> spec = new Moq.Mock<ISpecification<MockEntity>>().Object;

   // Passing null for the left specification
   Assert.That(() => new AndSpecification<MockEntity>(null, spec),
      Throws.TypeOf<ArgumentNullException>()
         .With.Property("ParamName").EqualTo("left"));

   // Passing null for the right specification
   Assert.That(() => new AndSpecification<MockEntity>(spec, null),
      Throws.TypeOf<ArgumentNullException>()
         .With.Property("ParamName").EqualTo("right"));

}

One single method can handle multiple cases.

You can find more details information from http://nunit.net/blogs/?p=63

Advertisements

Reuse is good!

One of the theme that I’ve always seen in design when people try to create some kind of “framework” is the heavily use of inheritance. Because one of the thing that people has been taught is “reuse is good”. Looks at the design below (noted that this design has nothing to do with DDD)

The same design is applied for repositories…

And the only reason to have this kind of design is for reusability, no infrastructure codes can easily deal with IService<T> or any other base classes in this design. (And there is no reason for infrastructure codes to deal with those things)

The next question is what happens if the UserService (or any service class) doesn’t need all functions implemented in the base class? What happens if there are some business rules need to be checked? (Then the workaround is to add more abstract methods around all functions in the base class)

The point here is: There is no reason to create a complex but not very useful design. My preferable design is something likes

We can still reuse some of the codes through composition but not inheritance, there is a well written blog post discuss almost the same issue here http://codebetter.com/blogs/gregyoung/archive/2009/01/16/ddd-the-generic-repository.aspx

When LTRIM() and RTRIM() do not work

When I analyzed the data from an Access database before import into our SQL database, I saw some strings with extra spaces at the end. So I thought I could simply use the RTRIM() function, I quickly put together a script and ran -> NOTHING WORKED

Tried couple times, the result still the same. It turns out there is a note in SQL Online Book

LTRIM does not remove white-space characters such as the tab or line feed characters…

And the spaces in the data are LFs (char(10)).  The solution is to replace those char(10) with a space (char(32)) before calling RTRIM()


UPDATE dbo.Cause_1
SET Result =  (RTRIM(REPLACE(Result, CHAR(10), CHAR(32))))

A more complete solution can be found at

http://blog.programmingsolution.net/sql-server-2008/removing-leading-and-trailing-white-space-characters-in-tsql/

 

Crystal Reports 64 bits issue

The error below happen on a 64 bits web server that tries to load the crystal reports run-time. If this sounds familiar with you and you already installed the “CRRedist2008_x64.msi”.  But it still didn’t work and you starting to get tired with google 🙂

You should look into your application pools settings, make sure you set the “Enable 32 bits Applications” to FALSE

UnityContainer registers itself!?

If you look at Prism 2.0 (Composite WPF), you will see the old codes have been commented out

// We register the container with an ExternallyControlledLifetimeManager to avoid
// recursive calls if Container.Dispose() is called.
// Container.RegisterInstance<IUnityContainer>(Container);

Base on this post http://compositewpf.codeplex.com/Thread/View.aspx?ThreadId=52999, that is because the container’s already registered itself.