SetValue 测试在我的 Period class 上成功,但在我的 Name class 上失败
SetValue test succeeds with my Period class but fails with my Name class
所以我的第一个 TestMethod 按预期通过了。我测试了我的 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.Valid
和 x.Id
是空的,尽管我明确地以与设置 p1.From
和 p1.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); }
}
}
编辑2:
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);
}
}
如果我在单元测试中调试您的代码,它可以正常工作。我将 setter 更改为正常使用,因为我不知道您在 Base
和 SetValue
中使用什么。
但是单元测试使用这段代码运行良好:
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
有效而其他 类 无效的原因。
所以我的第一个 TestMethod 按预期通过了。我测试了我的 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.Valid
和 x.Id
是空的,尽管我明确地以与设置 p1.From
和 p1.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); }
}
}
编辑2:
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);
}
}
如果我在单元测试中调试您的代码,它可以正常工作。我将 setter 更改为正常使用,因为我不知道您在 Base
和 SetValue
中使用什么。
但是单元测试使用这段代码运行良好:
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
有效而其他 类 无效的原因。