如何模拟继承的方法

发布于 2024-12-12 10:12:31 字数 1225 浏览 5 评论 0原文

我试图模拟一个从通用父类继承的方法。正确知道我的代码看起来像这样。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爺獨霸怡葒院 2024-12-19 10:12:31

问题是您正在比较 lambda - 但您确实希望将 person 实例传递到 lambda 与基于您的 person 对象相匹配满足谓词条件 - 您可以使用 Matches() 通过在 p 上执行谓词来实现此目的 - 如果等于 true 则你有一个匹配,应该返回存根列表:

mockPersonRepository.Stub(x => x.FindMany(Arg<Func<person, bool>>.Matches( y => y(p))))
                    .Return(new List<person> { p });

The problem is that you are comparing the lambda - but you are really interested in having the person instance passed into the lambda match your person object based on satisfying the predicate condition - You can use Matches() to achieve this by just executing the predicate on p - if that equates to true than you have a match and should return the stubbed list:

mockPersonRepository.Stub(x => x.FindMany(Arg<Func<person, bool>>.Matches( y => y(p))))
                    .Return(new List<person> { p });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文