我正在尝试克服一个类具有字符串构造函数参数的情况,该参数不能被Autofixture生成的任何旧字符串(Guid-y外观值)所满足.
在您想要简单地回复Mark Seemann的关于基于会议的自定义的Ploeh博客文章的链接之前,请允许我说我一直在引用它和他的其他博客条目进行此测试,我无法访问通过.
当我在调试中单步执行时,我可以看到构造函数参数在某些时候传入了有效值,但测试仍然失败并带有Guid-y Color值.我认为这与"自动混合"填充"颜色"参数值和 "颜色"属性这一事实有关.是不是我编写了一个解决构造函数参数的ISpecimenBuilder,但我正在测试公共属性值(两个不同的东西)?
我知道所有这些对于该示例来说都是过度的,但我想到了一个更复杂的场景,其中使用该Build<T>().With()方法将不会是DRY.
失败测试
[Fact]
public void Leaf_Color_Is_Brown()
{
// arrange
var fixture = new Fixture().Customize(new LeafColorCustomization());
// act
var leaf = fixture.Create<Leaf>();
// using .Build<>.With(), test passes
//var leaf = fixture.Build<Leaf>().With(l => l.Color, "brown").CreateAnonymous();
// assert
Assert.True(leaf.Color == "brown");
}
Run Code Online (Sandbox Code Playgroud)
SUT
public class Leaf
{
public Leaf(string color)
{
if (color != "brown")
throw new ArgumentException(@"NO LEAF FOR YOU!");
this.Color = color;
}
public string Color { get; set; } …Run Code Online (Sandbox Code Playgroud) 让我们考虑同一个非常简单的实体的两个版本(一个带有只读属性):
public class Client
{
public Guid Id { get; set; }
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
与
public class Client
{
public Client(Guid id, string name)
{
this.Id = id;
this.Name = name;
}
public Guid Id { get; }
public string Name { get; }
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用Autofixture时,它们将正常工作,并且可以同时使用它们。当我尝试使用预先定义参数之一时,问题就开始了。with()方法:
var obj = this.fixture.Build<Client>().With(c => c.Name, "TEST").Build();
Run Code Online (Sandbox Code Playgroud)
这将引发错误
System.ArgumentException:属性“名称”是只读的。
但是似乎Autofixture知道如何使用构造函数!而且看来实际Build<>()方法不是创建对象的实例Create()!如果构建仅准备具有设置属性的构建器,然后创建将实例化对象,则它将与只读属性一起正常工作。
那么为什么在这里使用这种(误导)策略呢?我在这里找到了一个答案,指出它是通过测试来放大反馈,但是我看不出使用的用处,FromFactory()尤其是当参数列表很广时。在Build()方法之间移动对象实例化Create()会更直观吗?
这是我的目标:
public class Symbol
{
private readonly string _identifier;
private readonly IList<Quote> _historicalQuotes;
public Symbol(string identifier, IEnumerable<Quote> historicalQuotes = null)
{
_identifier = identifier;
_historicalQuotes = historicalQuotes;
}
}
public class Quote
{
private readonly DateTime _tradingDate;
private readonly decimal _open;
private readonly decimal _high;
private readonly decimal _low;
private readonly decimal _close;
private readonly decimal _closeAdjusted;
private readonly long _volume;
public Quote(
DateTime tradingDate,
decimal open,
decimal high,
decimal low,
decimal close,
decimal closeAdjusted,
long volume)
{
_tradingDate = …Run Code Online (Sandbox Code Playgroud) 我有一个概念上看起来像这样的课程:
public class Entity
{
private readonly List<double> _values = new List<double>();
...
public List<double> Values
{
get
{
return _values;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在单元测试中,我想使用 AutoFixture 提供随机实体列表:
var entities = this.fixture.CreateMany<Entity>().ToList();
Run Code Online (Sandbox Code Playgroud)
但是,正如我所期望的(希望...),没有将自动生成的值添加到 Entity 对象的 Values 属性中。我尝试将值列表更改为非只读,并向 Values 属性添加一个设置器,这解决了问题,但是没有更好的替代方法吗?