尽管配置不同,为什么这些模拟对象相同?

时间:2013-06-05 15:30:19

标签: c#-4.0 autofac automocking

以下代码应该使用不同的电子邮件属性模拟两个对象。但相反,我得到两个具有相同电子邮件属性的对象。为什么呢?

using (var autoMock = AutoMock.GetStrict())
{
    var contact1 = autoMock.Mock<IContact>();
    contact1.SetupGet(x => x.Email).Returns("a");

    var contact2 = autoMock.Mock<IContact>();
    contact2.SetupGet(x => x.Email).Returns("b");

    Assert.AreNotEqual(contact1.Object.Email, contact2.Object.Email);
}

1 个答案:

答案 0 :(得分:0)

使用Mock类而不是AutoMock:

var contact1 = new Mock<IContact>();
contact1.SetupGet(x => x.Email).Returns(DefaultEmail1);

var contact2 = new Mock<IContact>();
contact2.SetupGet(x => x.Email).Returns(DefaultEmail2);

Assert.AreNotEqual(contact1.Object.Email, contact2.Object.Email);