简体   繁体   English

为什么不能更改Rhino Mocks存根对象的返回值?

[英]Why can't I change the return value in a Rhino Mocks stub object?

Forgive me if this is a stupid question, but I'm fairly new at mocking, and am trying to get my head around it. 如果这是一个愚蠢的问题,请原谅我,但我在嘲笑方面还很陌生,并且正在努力解决这个问题。

I have some unit tests (using the built-in Visual Studio 2010 Professional testing features), which use a stub that is needed by a method. 我有一些单元测试(使用内置的Visual Studio 2010 Professional测试功能),这些单元测试使用方法所需的存根。 I created a stub, and set the default return values for a couple of properties and a method, and all works well. 我创建了一个存根,并为几个属性和方法设置了默认的返回值,并且一切正常。 I have a static class that sets up the stub, and this is used in the TestInitialize method to set up the stub... 我有一个设置存根的静态类,它在TestInitialize方法中用于设置存根...

public static AppointmentReminderProviderInterface GetMockProvider() {
  AppointmentReminderProviderInterface provider = MockRepository.GenerateStub<AppointmentReminderProviderInterface>();
  provider.Stub(p => p.ContactName).Return(MockProviderContactName);
  provider.Stub(p => p.ContactEmail).Return(MockProviderContactEmail);
  return provider;
}

Note that MockProviderContactName and MockProviderContactEmail are local string properties that contain default data for the provider. 请注意, MockProviderContactNameMockProviderContactEmail是本地字符串属性,其中包含提供程序的默认数据。 The unit tsts that check to see if things work as expeced with the default data all pass fine. 检查默认数据是否按预期工作的单元tst全部合格。

However, I now want to test what happens when one of these properties contains duff data. 但是,我现在要测试当这些属性之一包含duff数据时会发生什么。 I thought I could just set this on the stub, but it isn't working. 我以为我可以在存根上设置它,但是它不起作用。 The test method contains the following lines... 测试方法包含以下几行...

_provider.Stub(p => p.ContactEmail).Return("invalid");
Debug.WriteLine("Provider email: <" + _provider.ContactEmail + ">");

The Debug.WriteLine() shows me that, despite the fact that I have set the ContactEmail property to return "invalid" it still returns the default email address. Debug.WriteLine()向我显示,尽管事实上我已将ContactEmail属性设置为返回“无效”,但它仍会返回默认电子邮件地址。 This causes my test to fail, as I'm expecting it to throw an exception, and it doesn't. 这导致我的测试失败,因为我期望它会引发异常,而事实并非如此。

Anyone any idea why I can't change the return value of this property? 有人知道为什么我不能更改此属性的返回值吗?

Thanks for any help. 谢谢你的帮助。

As a tricky but working solution I'd suggest to use Do handler here. 作为一个棘手但可行的解决方案,我建议在这里使用Do handler It allows to implement some logic for stubbed method/property. 它允许为存根方法/属性实现一些逻辑。

See example: 参见示例:

var providerContactEmail = "Email1";

provider
    .Stub(p => p.ContactEmail)
    .Do((Func<string>)(() => providerContactEmail));
// here provider.ContactEmail returns "Email1"

providerContactEmail = "Email2";
// now provider.ContactEmail returns "Email2"

But in my opinion it is much better if the solution can be found which allows to don't change return value in the middle of test. 但是我认为最好能找到一种解决方案,该解决方案允许在测试过程中不更改返回值。 :) :)

Try changing 尝试改变

_provider.Stub(p => p.ContactEmail).Return("invalid");

To

_provider.ContactEmail = "invalid";

Assuming ContactEmail has a setter, this should work. 假设ContactEmail有一个setter,这应该可以工作。 If this doesn't resolve your issue, I'd follow Alexander Stepaniuk's advice and refactor your test so that you're not adding multiple behaviors for the same stub; 如果这不能解决您的问题,我将遵循Alexander Stepaniuk的建议并重构您的测试,以便您不会为同一存根添加多个行为; I think that has something to do with your issue. 我认为这与您的问题有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM