2

I have a test method...

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

... and the test passes. However, I have not implemented the mapping of the DataEntity's properties to the MainViewModel's properties yet! How can this be? I thought AreSame checks whether two references point to the same instance.

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);
        }
    }
}
Riegardt Steyn
  • 5,431
  • 2
  • 34
  • 49
  • did you debug the values of `facilityDataEntity.Value` and `mainViewModel.FacilityValue` – FosterZ Mar 13 '13 at 08:18
  • The same question goes for Assert.ReferenceEquals – Riegardt Steyn Mar 13 '13 at 08:18
  • 4
    Presumably, they're both `null` at this point. – Damien_The_Unbeliever Mar 13 '13 at 08:20
  • I have debugged the test, and yes, they are both null, but I'm not doing an AreEqual, so how can they be the same instance? Or does them being null MAKE them the same instance, or instance-less or something? – Riegardt Steyn Mar 13 '13 at 08:23
  • 2
    They're not the same instance. They're the same lack of an instance. – Damien_The_Unbeliever Mar 13 '13 at 08:28
  • Just to clarify how to think about null in C#: It's just another value, meaning null == null. This is quite different from null i relational databases, where null is not a value but means "unknown", which implies very different logic than in C#. For example, in DB null = null evaluates to null but is coerced to false where a boolean value is required, e.g. in a where clause. – Kjell Rilbe Apr 11 '13 at 08:12

3 Answers3

7

Underneath it all, these tests are just using Object.ReferenceEquals:

true if objA is the same instance as objB or if both are null; otherwise, false.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
3

I guess this is happening because they are both null.

the_joric
  • 11,986
  • 6
  • 36
  • 57
2

in this case, I'd say its comparing null with null, which are the same.

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156