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

4

1 回答 1

1

您需要将方法设为虚拟的原因是因为您正在模拟一个直接类。解决此问题的最佳方法是创建一个部分类(如您建议的那样)并让该部分类继承一个接口。所以你会有类似的东西:
designer.cs -> public partial class UserLog
UserLog.cs -> public partial class UserLog : IUserLog
IUserLog.cs -> 应该实现的方法(方法将由 Designer.cs 实现)

部分您创建的类 (UserLog.cs) 只是为了实现 IUserLog。IUserLog 将包含所有需要实现的方法,例如:public UserProfile UserProfile()。

完成后,您可以模拟接口而不是类。
我希望这会有所帮助,但可能比我解释的要难。

于 2010-04-28T10:23:12.773 回答