如何模拟继承的方法
本文关键字:继承 方法 模拟 何模拟 | 更新日期: 2023-09-27 18:12:43
我试图模拟一个从父类继承的通用方法。我的代码是这样的
public interface IBaseRepository<T>
{
IEnumerable<T> FindMany(Func<T, bool> condition);
}
public interface IPersonRepository : IBaseRepository<person>
{
//Here I got some specifics methods for person repository
}
我的测试代码像这样;
private IPersonRepository mockPersonRepository { get; set; }
[TestMethod]
public void TestMehtod()
{
LogonModel model = CreateLogonModel("test@test.com", "test", "Index");
person p = new person() { Email = model.Email, password = model.Password, PersonId = 1 };
mockPersonRepository.Stub(x => x.FindMany(y => y.Email == model.Email && y.password == model.Password)).Return(new List<person> {p});
mockPersonRepository.Replay();
var actual = instanceToTest.LogOnPosted(model) as PartialViewResult;
Assert.AreEqual("_Login", actual.ViewName);
}
当我在vs 2010中使用调试工具时,我可以看到我的Stub,不工作,返回的人总是空的。我已经将FindMany方法声明为虚方法。
有人知道如何存根这个方法吗?我用的是RhinoMocks
问题是您正在比较lambda -但是您真正感兴趣的是将传递到lambda的person
实例匹配基于满足谓词条件的person
对象-您可以使用Matches()
通过在p
上执行谓词来实现这一点-如果这等同于true
,那么您有一个匹配并应返回存根列表:
mockPersonRepository.Stub(x => x.FindMany(Arg<Func<person, bool>>.Matches( y => y(p))))
.Return(new List<person> { p });