使用 Rhino Mocks 设置存根方法的返回值的技术差异

发布于 2024-09-02 20:15:40 字数 1912 浏览 3 评论 0原文

以下两种为方法提供虚假实现的方法之间的主要区别是什么?

我在一个测试中使用第二种方法很好,但在另一个测试中,除非我使用第一种方法,否则无法实现该行为。

所以(第一个),

using (test.Record()) //test is MockRepository instance
{
 service.GetUser("dummyName");
 LastCall.Return(new LoginUser());
}

vs(第二个)。

service.Stub(r => r.GetUser("dummyName")).Return(new LoginUser());

编辑

问题是,当我期望第二种技术返回新的 LoginUser 时,它在测试中返回 null。第一种技术的行为与预期一致,返回一个新的 LoginUser。两种情况下使用的所有其他测试代码都是相同的。

[TestFixture]
public class AuthorizationTest
{
    private MockRepository test;
    private IMembershipService service;

    [SetUp]
    public void SetUp()
    {
        test = new MockRepository();
        service = test.Stub<IMembershipService>();

        using (test.Record())
        {
            service.GetUser("dummyName");
            LastCall.Return(new LoginUser());
        }

        //service.Stub(r => r.GetUser("dummyName")).Return(new LoginUser()); 
    }

    [Test]
    public void GetCurrentUser_UserIsAuthenticated_ReturnsCurrentUser()
    {
        var authStub = new AuthorizationStub_SetCurrentUserAuthenticated(service, true);

        LoginUser u = authStub.GetCurrentUser();
        Assert.That(u != null);
    }

    [TearDown]
    public void TearDown()
    {
        service = null;
        test = null;
    }
}

可能与接口过载有关?

public interface IMembershipService
{
    bool ChangePassword(string username, string oldPassword, string newPassword);
    LoginUser GetUser(string username);
    LoginUser GetUser(object providerUserKey);
    string ResetPassword(string username);
    bool ValidateUser(string username, string password);
}

在测试方法中导致问题的行是:

LoginUser currentUser = _repository.GetUser(identity.Name);

在调试模式下,identity.Name 永远不会为 null,并且始终为“dummyName”

What is the main difference between these following two ways to give a method some fake implementation?

I was using the second way fine in one test but in another test the behaviour can not be achieved unless I go with the first way.

so (the first),

using (test.Record()) //test is MockRepository instance
{
 service.GetUser("dummyName");
 LastCall.Return(new LoginUser());
}

vs (the second).

service.Stub(r => r.GetUser("dummyName")).Return(new LoginUser());

Edit

The problem is that the second technique returns null in the test, when I expect it to return a new LoginUser. The first technique behaves as expected by returning a new LoginUser. All other test code used in both cases is identical.

[TestFixture]
public class AuthorizationTest
{
    private MockRepository test;
    private IMembershipService service;

    [SetUp]
    public void SetUp()
    {
        test = new MockRepository();
        service = test.Stub<IMembershipService>();

        using (test.Record())
        {
            service.GetUser("dummyName");
            LastCall.Return(new LoginUser());
        }

        //service.Stub(r => r.GetUser("dummyName")).Return(new LoginUser()); 
    }

    [Test]
    public void GetCurrentUser_UserIsAuthenticated_ReturnsCurrentUser()
    {
        var authStub = new AuthorizationStub_SetCurrentUserAuthenticated(service, true);

        LoginUser u = authStub.GetCurrentUser();
        Assert.That(u != null);
    }

    [TearDown]
    public void TearDown()
    {
        service = null;
        test = null;
    }
}

Could it be something to do with the overload in the interface perhaps?

public interface IMembershipService
{
    bool ChangePassword(string username, string oldPassword, string newPassword);
    LoginUser GetUser(string username);
    LoginUser GetUser(object providerUserKey);
    string ResetPassword(string username);
    bool ValidateUser(string username, string password);
}

The line causing the problem in the method under test is:

LoginUser currentUser = _repository.GetUser(identity.Name);

In debug mode, identity.Name is never null and is always "dummyName"

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

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

发布评论

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

评论(1

丑疤怪 2024-09-09 20:15:40

第二种方法是新的 AAA语法,并且是首选方法。也许您可以使用 AAA 语法扩展您遇到的问题,并且您也许能够获得解决该问题的帮助。

编辑:

您确定正在使用该参数调用该方法吗?如果您使用会发生什么情况,

 service.Stub(r => r.GetUser(Arg<String>.Is.Anything)).Return(new LoginUser());

您会得到任何回报吗?

如果您设定一个期望而不是仅仅将其存根,然后验证该期望,这会不会为您提供更多信息?

service.Expect(r => r.GetUser(Arg<String>.Is.Anything)).Return(new LoginUser());
service.VerifyAllExpectations();

EDIT2:

好的,我做了一些测试,看来这就是您创建存根对象的方式。我假设您使用的方式是针对旧样式的,并且您需要将其用于新样式:

service = MockRepository.GenerateStub<IMembershipService>();

当我更改为这种方式时,我可以运行测试并让它们通过。我不确定使用静态方法生成存根和使用测试 MockRepository 实例之间的确切区别。也许其他人可以解释它的来龙去脉。

无论如何,为了完整起见,这里是我运行的有效代码:

[TestFixture]
public class AuthorizationTest
    {
    private IMembershipService service;

    [SetUp]
    public void SetUp()
        {
        service = MockRepository.GenerateStub<IMembershipService>();
        service.Stub(r => r.GetUser("dummyName")).Return(new LoginUser()); 
        }

    [Test]
    public void GetCurrentUser_UserIsAuthenticated_ReturnsCurrentUser()
        {
        var authStub = new AuthorizationStub_SetCurrentUserAuthenticated(service, true);

        LoginUser u = authStub.GetCurrentUser();
        Assert.That(u != null);
        }

    [TearDown]
    public void TearDown()
        {
        service = null;
        }
    }

public class AuthorizationStub_SetCurrentUserAuthenticated
    {
    private readonly IMembershipService m_service;
    private readonly bool m_b;

    public AuthorizationStub_SetCurrentUserAuthenticated (IMembershipService service, bool b)
        {
        m_service = service;
        m_b = b;
        }

    public LoginUser GetCurrentUser ()
        {
        return m_service.GetUser ("dummyName");
        }
    }

public interface IMembershipService
    {
    bool ChangePassword(string username, string oldPassword, string newPassword);
    LoginUser GetUser(string username);
    LoginUser GetUser(object providerUserKey);
    string ResetPassword(string username);
    bool ValidateUser(string username, string password);
    }

public class LoginUser
    {
    }

the second method is the new AAA syntax, and is the preferred method. Perhaps you could expand on the problems you had using the AAA syntax, and you might be able to get help fixing that issue.

EDIT:

Are you sure that the method is being called with that parameter? What happens if you use

 service.Stub(r => r.GetUser(Arg<String>.Is.Anything)).Return(new LoginUser());

do you get anything returned?

What if you set an expectation rather than just stubbing it and then verify the expectation, might that give you some more information?

service.Expect(r => r.GetUser(Arg<String>.Is.Anything)).Return(new LoginUser());
service.VerifyAllExpectations();

EDIT2:

ok I did a bit of testing and it seems that it is the way you are creating your stub object. I assume that the way you are using is for the old style and that you need to use this for the new style:

service = MockRepository.GenerateStub<IMembershipService>();

When I changed to this I can run the tests and get them to pass. I'm not certain of the exact difference between using the static method to generate the stub and using the test MockRepository instance. Maybe someone else can explain the ins and outs of it.

Anyway for completeness here is the code I ran which worked:

[TestFixture]
public class AuthorizationTest
    {
    private IMembershipService service;

    [SetUp]
    public void SetUp()
        {
        service = MockRepository.GenerateStub<IMembershipService>();
        service.Stub(r => r.GetUser("dummyName")).Return(new LoginUser()); 
        }

    [Test]
    public void GetCurrentUser_UserIsAuthenticated_ReturnsCurrentUser()
        {
        var authStub = new AuthorizationStub_SetCurrentUserAuthenticated(service, true);

        LoginUser u = authStub.GetCurrentUser();
        Assert.That(u != null);
        }

    [TearDown]
    public void TearDown()
        {
        service = null;
        }
    }

public class AuthorizationStub_SetCurrentUserAuthenticated
    {
    private readonly IMembershipService m_service;
    private readonly bool m_b;

    public AuthorizationStub_SetCurrentUserAuthenticated (IMembershipService service, bool b)
        {
        m_service = service;
        m_b = b;
        }

    public LoginUser GetCurrentUser ()
        {
        return m_service.GetUser ("dummyName");
        }
    }

public interface IMembershipService
    {
    bool ChangePassword(string username, string oldPassword, string newPassword);
    LoginUser GetUser(string username);
    LoginUser GetUser(object providerUserKey);
    string ResetPassword(string username);
    bool ValidateUser(string username, string password);
    }

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