3

Say I have an interface like this.

public interface ICamProcRepository
{
    List<IAitoeRedCell> GetAllAitoeRedCells();
    IAitoeRedCell CreateAitoeRedCell();
}

How do I mock the method which return an interface and a list of interface objects. I am using Ninject.MockingKernel.Moq

var mockingKernel = new MoqMockingKernel();

var camProcRepositoryMock = mockingKernel.GetMock<ICamProcRepository>();

camProcRepositoryMock.Setup(e => e.GetAllAitoeRedCells()).Returns(?????WHAT HERE?????);

camProcRepositoryMock.Setup(e => e.CreateAitoeRedCell()).Returns(?????WHAT HERE?????);
4

1 回答 1

2

在您的情况下,您需要为有问题的模拟接口创建所需结果的模拟,并将它们Returns通过内核或直接 moq 传递到其设置中。我不知道 Ninject 能够在那里为您提供帮助,但这是一个简单的起订量示例

var mockingKernel = new MoqMockingKernel();

var camProcRepositoryMock = mockingKernel.GetMock<ICamProcRepository>();

var fakeList = new List<IAitoeRedCell>();
//You can either leave the list empty or populate it with mocks.
//for(int i = 0; i < 5; i++) {
//    fakeList.Add(Mock.Of<IAitoeRedCell>());
//}

camProcRepositoryMock.Setup(e => e.GetAllAitoeRedCells()).Returns(fakeList);

camProcRepositoryMock.Setup(e => e.CreateAitoeRedCell()).Returns(() => Mock.Of<IAitoeRedCell>());

如果模拟对象也需要提供假功能,那么也必须相应地设置。

于 2016-09-03T18:38:52.140 回答