简体   繁体   English

在创建模拟时如何组合两个接口?

[英]How do I combine two interfaces when creating mocks?

We are using Rhino Mocks to perform some unit testing and need to mock two interfaces. 我们正在使用Rhino Mocks执行一些单元测试,需要模拟两个接口。 Only one interface is implemented on the object and the other is implemented dynamically using an aspect-oriented approach. 在对象上只实现了一个接口,而另一个接口是使用面向方面的方法动态实现的。 Is there an easy way to combine the two interfaces dynamically so that a mock can be created and the methods stubbed for both interfaces? 是否有一种简单的方法可以动态组合这两个接口,以便可以创建模拟并为两个接口创建方法?

Using Rhino Mocks 使用Rhino Mocks

var mock = MockRepository.GenerateMock<IFirst, ISecond>();
mock.Stub(m => m.FirstProperty).PropertyBehavior();
((ISecond)mock).Stub(k=> k.SecondProperty).PropertyBehavior();

Found and used the information from http://www.richard-banks.org/2010/08/mocking-comparison-part-11-multiple.html 找到并使用了来自http://www.richard-banks.org/2010/08/mocking-comparison-part-11-multiple.html的信息

It's not dynamic, but certainly easy: create an interface within your testing assembly which does nothing other than implementing the other two interfaces: 它不是动态的,但肯定很简单:在测试程序集中创建一个接口,除了实现其他两个接口之外什么都不做:

internal interface ICombined : IFirstInterface, ISecondInterface {}

Then mock ICombined . 然后模拟ICombined

A mock with multiple interfaces using Rhino Mocks can be generated like so: 使用Rhino Mocks的多个接口的模拟可以像这样生成:

var mocker = new MockRepository();
var mock = mocker.CreateMultiMock<IPrimaryInterface>(typeof(IFoo), typeof(IBar));
mocker.ReplayAll();

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

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