2

我有一个测试方法...

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

3 回答 3

7

在这一切之下,这些测试只是使用Object.ReferenceEquals

如果objA是与objB相同的实例,或者两者都为null ,则为true ;否则,false

于 2013-03-13T08:30:55.837 回答
3

我想这正在发生,因为它们都是null.

于 2013-03-13T08:21:18.643 回答
2

在这种情况下,我会说它比较 null 和 null,它们是相同的。

于 2013-03-13T08:18:09.257 回答