如何模拟继承的方法
我试图模拟一个从通用父类继承的方法。正确知道我的代码看起来像这样。
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("[email protected]", "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。
Im trying to mock a method that is ineherited from a parent class that is generic. Right know my code looks like this.
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
}
My test code looks like this;
private IPersonRepository mockPersonRepository { get; set; }
[TestMethod]
public void TestMehtod()
{
LogonModel model = CreateLogonModel("[email protected]", "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);
}
When I am using the debugging tool in vs 2010 I can se that me Stub, doesnt works, the return person is always null. I have declared the FindMany method as virtual.
Does anybody know how to stub that method? Im using RhinoMocks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在比较 lambda - 但您确实希望将
person
实例传递到 lambda 与基于您的person
对象相匹配满足谓词条件 - 您可以使用Matches()
通过在p
上执行谓词来实现此目的 - 如果等于true
则你有一个匹配,应该返回存根列表:The problem is that you are comparing the lambda - but you are really interested in having the
person
instance passed into the lambda match yourperson
object based on satisfying the predicate condition - You can useMatches()
to achieve this by just executing the predicate onp
- if that equates totrue
than you have a match and should return the stubbed list: