2013-06-05 85 views
1

以下代碼應該模擬具有不同Email屬性的兩個對象。但是,我得到了兩個具有相同電子郵件屬性的對象。爲什麼?爲什麼這些模擬對象是相同的,雖然配置不同?

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); 
} 

回答

0

使用模擬類,而不是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); 
相關問題