TDD - 为什么Assert.AreSame会通过?

时间:2013-03-13 08:13:48

标签: c# tdd mstest assert

我有一种测试方法......

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

3 个答案:

答案 0 :(得分:7)

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

  

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

答案 1 :(得分:3)

我想这是因为它们都是null

答案 2 :(得分:2)

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