在没有公共构造函数的情况下模拟密封类? [英] Mocking sealed classes without public constructors?

查看:32
本文介绍了在没有公共构造函数的情况下模拟密封类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在测试的特定类取决于 HttpSessionState 对象.

The particular class I'm testing depends upon the HttpSessionState object.

HttpSessionState 类没有公共构造函数.被测类仅将此对象用作 NameValue 存储.该类在 ASMX Web 服务中用于返回特定方法的信息.

The HttpSessionState class has no public constructors. The class under test is only using this object as a NameValue store. The class is used in an ASMX web service to return information for a particular method.

我正在考虑围绕 HttpSessionState 类创建一个外观,我可以在其中提供一个 Dictionary <string, string>而不是测试中的 Session 对象.

I'm thinking about creating a facade around the HttpSessionState class where I can provide a Dictionary <string, string> instead of the Session object in testing.

这是一个好主意还是标准做法?

Is this a good idea or standard practice?

推荐答案

是的,正如老话说的那样,没有什么是通过添加另一个抽象层无法解决的.我通常只是将类型隐藏在接口后面,其中接口的方法是在该类型上执行我想要的操作所需的唯一方法.

Yep, as the old saying goes, there's nothing that can't be solved by adding another layer of abstraction. I usually just hide the type behind an interface where the interface's methods are the only ones needed to perform the actions I want on that type.

只模拟隐藏HttpSessionState的接口,并对接口的使用做断言,在Rhino Mocks中只是AssertWasCalled(d => ....)等

Just mock the interface that hides HttpSessionState, and do Asserts on the uses of the interface, in Rhino Mocks it's just AssertWasCalled(d => ....) etc.

这篇关于在没有公共构造函数的情况下模拟密封类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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