gpt4 book ai didi

c# - SetValue 测试在我的 Period 类上成功但在我的 Name 类上失败

转载 作者:行者123 更新时间:2023-11-28 20:52:05 27 4
gpt4 key购买 nike

所以我的第一个 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.ANamex.Validx.Id 是空的,尽管我明确地以与设置 p1 相同的方式设置它们。从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 更改为正常使用,因为我不知道您在 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 工作而其他类不工作的原因。

关于c# - SetValue 测试在我的 Period 类上成功但在我的 Name 类上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42668881/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com