存根未使用 Rhino Mocks 3.6 返回正确的值

发布于 2024-09-01 01:41:42 字数 1018 浏览 1 评论 0原文

我正在尝试使用 Rhino Mocks 3.6 和 AAA 编写一个测试。我遇到的问题是我设置的存根似乎没有返回正确的对象。

以下测试失败:

    [SetUp]
    public void SetUp()
    {
        repository = new MockRepository();
        webUserDal = repository.Stub<IWebUserDal>();
    }

    [Test]
    public void Test()
    {
        var user1 = new WebUser{Status = Status.Active, Email = "[email protected]"};
        webUserDal.Stub(x => x.Load(Arg<string>.Is.Anything)).Return(user1);

        var user2 = webUserDal.Load("[email protected]");

        Assert.AreEqual(user1.Email, user2.Email);
    }

User1 的电子邮件属性为 [email protected],而 user2 的电子邮件财产为空

任何人都可以阐明我做错了什么吗?

I'm trying to write a test using Rhino Mocks 3.6 with AAA. The problem I'm running into is that the Stub i've set up doesn't seem to be returning the correct object.

The following test fails:

    [SetUp]
    public void SetUp()
    {
        repository = new MockRepository();
        webUserDal = repository.Stub<IWebUserDal>();
    }

    [Test]
    public void Test()
    {
        var user1 = new WebUser{Status = Status.Active, Email = "[email protected]"};
        webUserDal.Stub(x => x.Load(Arg<string>.Is.Anything)).Return(user1);

        var user2 = webUserDal.Load("[email protected]");

        Assert.AreEqual(user1.Email, user2.Email);
    }

User1's email property is [email protected] while user2's email property is null

Could anyone shed some light on what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

有木有妳兜一样 2024-09-08 01:41:42

您混淆了新旧语法,并且它似乎不能很好地协同工作。如果您想使用新语法(首选),则必须将设置方法更改为:

[SetUp]
public void SetUp()
{
    webUserDal = MockRepository.GenerateStub<IWebUserDal>();
}

如果创建 MockRepository 对象,则需要在使用模拟之前运行repository.ReplayAll(),但这是旧语法。所以最好只使用静态方法。

You mixed up the old and new syntax, and it doesn't seem to work nicely together. If you want to use the new syntax (preferred), you have to change your set up method to:

[SetUp]
public void SetUp()
{
    webUserDal = MockRepository.GenerateStub<IWebUserDal>();
}

If you create the MockRepository object then you need to run repository.ReplayAll() before you use the mocks, but this is the old syntax. So its better to just use static methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文