Search code examples
c#unit-testingmolespex

Can Moles be used to create a sealed class?


I am trying to mock my Linq To SQL classes.

I have the following code:

IQueryable<User> vUser =
     (from aUser in _ctx.Users
      where aUser.UserName == userName
      select aUser);

Clearly while doing a unit test _ctx.Users is null. I can mock _ctx (the data context), but Users is a Table<T> and is sealed. So this fails:

_ctx.Users = Table<User>();

Pex talks about being able to divert any method call via the use of Moles.

I see examples letting you override DateTime.Now(), but I can't see if this would help in my scenario.

Any one have experience with this?


Solution

  • Yes it can.

    See this post for more info.

    http://social.msdn.microsoft.com/Forums/en-US/pex/thread/d73f730b-37df-40f9-8c62-afb76993fbbe/#86699ca1-70e6-4026-a1f9-4978e286d0a3