views:

104

answers:

1

My datacontext contains a table named Userlog with a one to many relationship with table UserProfile.

In class UserLog

public UserProfile UserProfile
{
  get {return this._UserProfile.Entity;}
}

In class UserProfile

public EntitySet<UserLog> UserLogs
{
  get{return this._UserLogs;}
}
{
  set {this._UserLogs.Assign(value);
}

How would I go about mocking (using Moq) UserLog.UserProfile without changing the autogenerated code?

What I would like to do is something like:

var mockUserLog = new Mock<UserLog>();
mockUserLog.Setup(c=>c.UserProfile.FirstName).Returns("Fred");

etc

I can do this if I go into the designer.cs and make FirstName and UserProfile virtual, however I would like to do this in the partial class.

Any ideas?

Thanks Jeremy

+1  A: 

The reason you need to make the method virtual is because you are mocking a direct class. Best way to approach this is to make a partial class (like you suggested) and let that partial class inheret an interface. So you will have something like:
designer.cs -> public partial class UserLog
UserLog.cs -> public partial class UserLog : IUserLog
IUserLog.cs -> methods that should be implemented (methods will be implemented by the designer.cs)

The partial class you create (UserLog.cs) is only there to make it possible to implement IUserLog. IUserLog will then contain all the methods that need to be implemented, like: public UserProfile UserProfile().

Once you've done that you can mock the interface instead of the class.
I hope this helped, it might be more difficult then I explained though.

Bas