无法使用 Rhino Mocks 模拟具有数组参数的构造函数的类
我们无法在 RhinoMocks 中模拟此类。
public class Service
{
public Service(Command[] commands){}
}
public abstract class Command {}
// Code
var mock = MockRepository.GenerateMock<Service>(new Command[]{}); // or
mock = MockRepository.GenerateMock<Service>(null)
Rhino 模拟失败,抱怨它找不到具有匹配参数的构造函数。 我做错了什么?
谢谢,
We are not able to mock this class in RhinoMocks.
public class Service
{
public Service(Command[] commands){}
}
public abstract class Command {}
// Code
var mock = MockRepository.GenerateMock<Service>(new Command[]{}); // or
mock = MockRepository.GenerateMock<Service>(null)
Rhino mocks fails complaining that it cannot find a constructor with matching arguments.
What am I doing wrong?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试这样:
Try like this:
此外,您可以使用接口包装 Service,而不必担心构造函数参数。如果构造函数发生变化——您的测试将与这些实现细节相关联并且需要更新。
编辑:至少隔离该模拟的创建,这样如果服务上的构造函数发生更改,您就不必在每个地方进行更新。常见的做法如下:(
在您的测试类中)
然后在测试中,
Additionally, you could wrap Service with an interface and not worry about the constructor arguments. If the constructor ever changes -- your tests will be tied to those implementation details and need to be updated.
Edit: At least isolate the creation of that Mock so if your constructor on Service changes, you won't have to update in every single place. A common practice is as follows:
(in your test class)
Then in a test,