我有一个在构造函数中设置字段的简单类,匹配属性是只读的。
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不为null并且属性上的get工作正常。我刚开始练习MSpec / Moq / Unit Testing,尽管到目前为止我已经阅读了很多关于这些主题的内容。任何帮助表示赞赏!