我是moq和单元测试的新手。所以,我不完全理解它。如果问题是愚蠢的,请原谅我。请帮助我了解以下情况。单元测试抛出“System.NullReferenceException”,使用Moq和Nunit
以下是我简单的测试
[Test]
public void TryMoq() {
var mock = new Mock<IDummyInterface>();
var dummy = new DummyClass(mock.Object);
mock.VerifySet(m => m.Model = It.Is<DummyModel>(mo => mo.MyProperty == "foo"));
}
和我米试图测试
public class DummyClass
{
public DummyClass(IDummyInterface i) {
i.Model = new DummyModel() ;
i.Model.MyProperty = "foo";
}
}
public class DummyModel
{
public string MyProperty { get; set; }
}
public interface IDummyInterface {
DummyModel Model { get; set; }
}
现在行 “i.Model.MyProperty = ”富代码“;” “System.NullReferenceException”正在被抛出。
为什么我认为自从我使用Moq以来的原因。
奇怪的是,如果我改变了“DummyClass”的构造函数。像这样
public DummyClass(IDummyInterface i)
{
i.Model = new DummyModel() { MyProperty ="foo"};
//i.Model.MyProperty = "foo";
}
测试通行证。在第二种情况下,即使我尝试将“foo”的值更改为“bar”。测试失败(虽然这很好)。
我只是想了解发生了什么事情。以及我想如何检查和验证孩子的财产。
感谢您的好解释。随你的答案。我也很喜欢“arneeiri”的回复,因为它解决了我的问题:) – Mohit 2010-06-30 21:43:34