Partial Layout Expectations - NullReference Exception

I have a partial mock problem using Rhino Mocks:

var authentication = (FormsAuthenticationService)_mocks.PartialMock(
  typeof(FormsAuthenticationService));
Expect.Call( delegate{ authentication.SetAuthCookie(null, null); }).IgnoreArguments();

      

.. and I get a NullReferenceException on "Expect". line..

I'll just add that FormsAuthenticationService

implementsIAuthentication

0


source to share


1 answer


Is there a good reason why you are trying to mock the physical class and not the interface? I ask this because there are two potential problems with the FormsAuthenticationService mock:

  • The class may not have a default parameterless constructor (in which case you need to provide an overloaded mocks.PartialMock method).

  • SetAuthCookie must be virtual. Layout frames can usually only mock unprintable classes and only virtual members of such a class.



To get around these issues, I would recommend mocking IAuthentication. We have no such restrictions. Here's the code you'll write:

var authentication = _mocks.DynamicMock<IAuthentication>();
Expect.Call(() => authentication.SetAuthCookie(null, null)).IgnoreArguments();

      

+1


source







All Articles