在Rhino.Mocks中添加一个字典 [英] Stubbing a dictionary in Rhino.Mocks

查看:158
本文介绍了在Rhino.Mocks中添加一个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个依赖关系的只读字典,我希望能够使用返回值存根,并检查它的分配是否发生。

I have a read-only dictionary on a dependency that I'd like to be able to stub with return values, and check that assignments to it have happened.

我希望Rhino.Mocks默认为我创建一个空字典,但不幸的是没有。由于它是只读的,我无法创建一个新的字典并将其分配给该属性。

I was hoping that Rhino.Mocks would create an empty dictionary for me by default, but unfortunately it doesn't. Since it is read-only, I can't create a new dictionary and assign it to that property.

我希望能够存储它。根据我的理解,C#语法可以看起来像这样:

I was hoping to be able to stub it instead. From what I understand, the C# syntax for this would looking something like this:

m.Stub(x => x.myProperty).Return("abc");

所以我希望这将适用于VB:

So I was hoping that this would work for VB:

m.Stub(sub(x) x.myProperty).Return("abc");

但它不(编译器错误)。关于如何实现这一点的任何想法?我可以使用Expect / Verify语法,如果可以完成这个...

But it doesn't (compiler error). Any ideas on how to accomplish this? I'm open to the Expect/Verify syntax if it can accomplish this...

推荐答案

使用函数将会诀窍:

m.Stub(Function(x) x.myProperty).Return("abc")

如果要验证 myProperty 被调用可以使用 Expect 而不是 Stub

m.Expect(Function(x) x.myProperty).Return("abc")

// Some code here

m.VerifyAllExpectations()

这篇关于在Rhino.Mocks中添加一个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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