为什么我不能在Rhino Mocks存根对象中更改返回值? [英] Why can't I change the return value in a Rhino Mocks stub object?

查看:96
本文介绍了为什么我不能在Rhino Mocks存根对象中更改返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个愚蠢的问题,请原谅我,但是我在嘲笑方面还很陌生,并且正在努力解决这个问题.

Forgive me if this is a stupid question, but I'm fairly new at mocking, and am trying to get my head around it.

我有一些单元测试(使用内置的Visual Studio 2010 Professional测试功能),这些单元测试使用方法所需的存根.我创建了一个存根,并为几个属性和一个方法设置了默认的返回值,并且一切正常.我有一个设置存根的静态类,在TestInitialize方法中用于设置存根...

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. 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...

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;
}

请注意,MockProviderContactNameMockProviderContactEmail是包含提供程序默认数据的本地字符串属性.用于检查事物是否使用默认数据按预期运行的单位tst全部通过.

Note that MockProviderContactName and MockProviderContactEmail are local string properties that contain default data for the provider. The unit tsts that check to see if things work as expeced with the default data all pass fine.

但是,我现在想测试当这些属性之一包含达夫数据时会发生什么.我以为我可以在存根上设置它,但是它不起作用.测试方法包含以下几行...

However, I now want to test what happens when one of these properties contains duff data. 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 + ">");

Debug.WriteLine()向我显示,尽管事实上我已将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. 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?

感谢您的帮助.

推荐答案

作为一个棘手但可行的解决方案,我建议在此处使用 Do handler .它允许为存根方法/属性实现一些逻辑.

As a tricky but working solution I'd suggest to use Do handler here. It allows to implement some logic for stubbed method/property.

查看示例:

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. :)

这篇关于为什么我不能在Rhino Mocks存根对象中更改返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆