DoUNo: Setting expectations on nullable type, NMock2
Ever had a problem of setting an expectation for nullable objects?
If you wanna return false when .HasValue of a nullable object is called, then you cannot do it with the normal expect statement. Rather, try not returning any values, because NMock2 returns default value of HasValue (False) if nothing is set as return values in expect statements.
Here is a sample,
public interface IProduct //Interface that has a nullable member { int? ProductNo //Member that I wanna test and I wish to test the scenario in which this will be null { get; } }
IProduct aProduct= myMockery.NewMock<IProduct>(); //Stub.On(aProduct).GetProperty("ProductNo").Will(Return.Value(default(int?)); //this statement produces a runtime exception, so we have to use the following instead Stub.On(aProduct).GetProperty("ProductNo"); //no return value is set, nmock2 returns false when .HasValue is queries
Hope this helped you.
Sudarsan Srinivasan
- on behalf of my friends (they found this hack
)

