1

我的[TestInitialize]块中有以下代码,并试图初始化List<T>我的对象的属性。

[TestClass]
public class MyTest
{
    private MyClass myClass;
    private Mock<myClassA> mockMyClassA;
    private List<Mock<myClassB>> mockMyClassB; //OK

    [TestInitialize]
    public void Setup()
    {
        mockMyClassA = new Mock<myClassA>();
        mockMyClassB = new List<Mock<myClassB>>(); //OK


        myClass = new MyClass
        {
            MyClassA = mockMyClassA.Object,
            MyClassB = mockBenefitsCoordination //None of this works
            {
                new Mock<mockMyClassB>()
                    {
                        //PropertyValue = mock??                            
                    }                    
            }
        }

我得到的错误是:

Cannot convert source type 'System.Collections.Genric.List<Moq.Mock<MyClassB>>' to target type 'System.Collections.Generic.IList<MyClassB>>'

好的,我看到List<T> to IList<T>了问题。MyClassB是一个IList<T>。我试过改变它没有成功。基本上,我认为我拥有使用 Moq 配置 List 属性的完整语法。有谁知道如何正确配置它?

4

1 回答 1

2

You don't want to initialize the list with the mock. You want to do the list initialization with the concrete object and add the mocked objects to that list.

Something like

private List<myClassB> myClassBList;
myClassBList.Add(new Mock<myClassB>.Object);
于 2013-03-08T19:10:54.427 回答