1

我有一个简单的类,它在构造函数中设置一个字段,并且匹配属性是只读的。

public class COMAssembly : ICOMAssembly
{
    private List<string> properties = new List<string>();
    public List<string> Properties
    {
        get { return properties; }
    }

    ...
}

我有这些使用 MSpec 和 Moq 的测试类:

using Machine.Specifications;
using Moq;
using System.Collections.Generic;
using It = Machine.Specifications.It;

public class MSPEC_With_a_COM_Assembly
{
    protected static Mock<ICOMAssembly> _mockCOMAssembly;
    protected static List<string> _listOfStrings;

    Establish context = () =>
       {
           _mockCOMAssembly = new Mock<ICOMAssembly>(MockBehavior.Loose);
           _mockCOMAssembly.DefaultValue = DefaultValue.Mock;
           _mockCOMAssembly.SetupAllProperties();
           _mockCOMAssembly.SetupProperty(m => m.Properties, new List<string>() { "Prop1", "Prop2" });
       };
}

[Subject(typeof(MSPEC_With_a_COM_Assembly), "With a COM Assembly")]
public class When_asking_for_a_list_of_properties_and_assembly_has_properties : MSPEC_With_a_COM_Assembly
{
    Because of = () =>
      {
          _listOfStrings = _mockCOMAssembly.Object.Properties;
      };

    It Should_return_a_list_with_values = () =>
        {
            //TODO:  Verify that the Count property of _listOfStrings/_mockCOMAssembly.Object.Properties is greater than zero.
        };
}

我查看了许多论坛、Moq 教程等,但无法找到仅使用 Moq 来解决此问题的答案。我不想使用 NUnit。我有其他通过验证 _listOfStrings 不为空并且该属性上的 get 工作正常的测试。我是练习 MSpec/Moq/单元测试的新手,尽管到目前为止我已经阅读了很多关于这些主题的内容。任何帮助表示赞赏!

4

0 回答 0