2017-03-08 27 views
1

所以我的第一個TestMethod經歷了,如預期。我測試了我的SetValue方法。SetValue測試成功與我的期間類,但失敗與我的名字類

p1 = new DateTimePeriod { From = DateTime.MinValue, To = DateTime.MaxValue }; 
p2 = new DateTimePeriod { From = DateTime.MaxValue, To = DateTime.MinValue }; 

[TestMethod] 
public void SetPeriodTest() 
{ 
    b.SetValue(ref p1, p2); 
    Assert.AreEqual(DateTime.MaxValue, p1.From); 
    Assert.AreEqual(DateTime.MinValue, p1.To); 
} 

但是下一次測試失敗。顯然x.AName,x.Validx.Id爲空,雖然我明確地設置它們與我設置p1.Fromp1.To相同的方式。爲什麼這些屬性返回null?

[TestMethod] 
public void SetNameTest() 
{ 
    var x = new Name { AName = "Jack", Valid = p1, Id = "123" }; 
    var y = new Name { AName = "John", Valid = p2, Id = "321" }; 
    b.SetValue(ref x, y); 
    Assert.AreEqual("John", x.AName); 
    Assert.AreEqual(p2, x.Valid); 
    Assert.AreEqual("321", x.Id); 
} 

編輯:

public class Name: Base 
{ 
    private string id; 
    public string Id 
    { 
     get { return id; } 
     set { SetValue(ref id, value); } 
    } 
    private string aName; 
    public string AName 
    { 
     get { return aName; } 
     set { SetValue(ref aName, value); } 
    } 
    private DateTimePeriod valid; 
    public DateTimePeriod Valid 
    { 
     get { return valid; } 
     set { SetValue(ref valid, value); } 
    } 
} 

public class DateTimePeriod: Period<DateTime> 
{ 
} 

public class Period<T>: Base 
{ 
    private T from; 
    private T to; 
    public Period() 
    { 
     from = default(T); 
     to = default(T); 
    } 
    public T From 
    { 
     get { return from; } 
     set { SetValue(ref from, value); } 
    } 
    public T To 
    { 
     get { return to; } 
     set { SetValue(ref to, value); } 
    } 
} 

EDIT2:

public class Base: object 
{ 
    public void SetValue<T>(ref T field, T value) 
    { 
     if (field == null) return; 
     if (field.Equals(value)) return; 
     field = value; 
     DoOnChanged(); 
    } 
    public event EventHandler OnChanged; 
    protected void DoOnChanged() 
    { 
     if (OnChanged == null) return; 
     OnChanged(this, null); 
    } 
} 
+0

只是x.AName等等null或y嗎? – SeeuD1

+0

兩者都是空... –

回答

0

如果我調試代碼在單元測試,it's工作沒有任何問題。我將二傳手改爲正常使用,因爲我不知道您使用的是BaseSetValue

但單元測試與此代碼工作正常:

namespace SampleUnitTest 
{ 
    [TestClass] 
    public class Foo 
    { 
     [TestMethod] 
     public void Bar() 
     { 
      var p1 = new DateTimePeriod { From = DateTime.MinValue, To = DateTime.MaxValue }; 
      var p2 = new DateTimePeriod { From = DateTime.MaxValue, To = DateTime.MinValue }; 
      var x = new Name { AName = "Jack", Valid = p1, Id = "123" }; 
      var y = new Name { AName = "John", Valid = p2, Id = "321" }; 
      Assert.AreEqual("John", x.AName); 
      Assert.AreEqual(p2, x.Valid); 
      Assert.AreEqual("321", x.Id); 
     } 
    } 

    public class Name 
    { 
     private string id; 
     public string Id 
     { 
      get { return id; } 
      set { id = value; } 
     } 
     private string aName; 
     public string AName 
     { 
      get { return aName; } 
      set { aName = value; ; } 
     } 
     private DateTimePeriod valid; 
     public DateTimePeriod Valid 
     { 
      get { return valid; } 
      set { valid = value ; } 
     } 
    } 

    public class DateTimePeriod : Period<DateTime> 
    { 
    } 

    public class Period<T> 
    { 
     private T from; 
     private T to; 
     public Period() 
     { 
      from = default(T); 
      to = default(T); 
     } 
     public T From 
     { 
      get { return from; } 
      set { from = value; } 
     } 
     public T To 
     { 
      get { return to; } 
      set { to = value; } 
     } 
    } 
} 

我覺得SetValue也是爲什麼你DateTime是工作,你的其他類鴕鳥政策的原因。

+0

我讓他們初始化。它們實際上位於[TestInitialize]中,但代碼不能與AName和Id一起使用。 –

+0

你可以發佈你的完整testmethod嗎? – SeeuD1

+0

基本上和你的答案一樣。我無法理解爲什麼DateTimePeriod(包含DateTime值)有效,但Name(包含Strings和DateTime)沒有。使用不同類型的類不允許我提供屬性值,但僅使用DateTime值的類完美運行。 –

相關問題