2013-03-13 47 views
2

我有一個測試方法...TDD - 爲什麼這個Assert.AreSame通過?

[TestMethod] 
public void MainViewModel_PropertiesReflectDataEntityProperties() 
{ 
    // Arrange 
    var facilityDataEntity = MockRepository.GenerateStub<FacilityDataEntity>(); 
    var shopOrderDataEntity = MockRepository.GenerateStub<ShopOrderDataEntity>(); 

    // Act 
    MainViewModel mainViewModel = new MainViewModel(facilityDataEntity, shopOrderDataEntity); 

    // Assert 
    Assert.AreSame(facilityDataEntity.Value, mainViewModel.FacilityValue); 
} 

...並測試通過。但是,我還沒有將DataEntity的屬性映射到MainViewModel的屬性呢!怎麼會這樣?我認爲AreSame檢查兩個引用是否指向同一個實例。

public class MainViewModel 
{ 
    private readonly FacilityDataEntity facilityDataEntity; 
    private readonly ShopOrderDataEntity shopOrderDataEntity; 

    public MainViewModel(FacilityDataEntity facilityDataEntity) 
    { 
     this.facilityDataEntity = facilityDataEntity; 
    } 

    public MainViewModel(FacilityDataEntity facilityDataEntity, ShopOrderDataEntity shopOrderDataEntity) 
    { 
     this.facilityDataEntity = facilityDataEntity; 
     this.shopOrderDataEntity = shopOrderDataEntity; 
    } 

    public ShopOrderDataEntity ShopOrderDataEntity 
    { 
     get { return shopOrderDataEntity; } 
    } 

    public FacilityDataEntity FacilityDataEntity 
    { 
     get { return facilityDataEntity; } 
    } 

    public int ShopOrder { get; set; } 

    public decimal RequiredQuantity { get; set; } 

    public string ItemCode { get; set; } 

    public string ItemDescription { get; set; } 

    public string FacilityValue { get; set; } 

    public string FacilityLabel { get; set; } 

    public static IEnumerable<MainViewModel> TranslateDataEntityList(IEnumerable<FacilityDataEntity> facilityDataEntityList) 
    { 
     foreach (FacilityDataEntity facilityDataEntity in facilityDataEntityList) 
     { 
      yield return new MainViewModel(facilityDataEntity); 
     } 
    } 

    public static IEnumerable<MainViewModel> TranslateDataEntityList(FacilityDataEntity facilityDataEntity, IEnumerable<ShopOrderDataEntity> shopOrderDataEntityList) 
    { 
     foreach (ShopOrderDataEntity shopOrderDataEntity in shopOrderDataEntityList) 
     { 
      yield return new MainViewModel(facilityDataEntity, shopOrderDataEntity); 
     } 
    } 
} 
+0

你調試facilityDataEntity.Value'的'價值觀和'mainViewModel.FacilityValue' – FosterZ 2013-03-13 08:18:11

+0

同樣的問題也適用於Assert.ReferenceEquals – Heliac 2013-03-13 08:18:26

+4

據推測,他們在這一點上兩者'null'。 – 2013-03-13 08:20:49

回答

6

在一切僞裝之下,這些測試只是使用Object.ReferenceEquals

真正如果objA是同一個實例objB或者如果兩者都;否則,虛假

2

在這種情況下,我會說它比較null與null,這是相同的。

3

我猜這是因爲他們都是null