这是我的对象:
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 = tradingDate;
_open = open;
_high = high;
_low = low;
_close = close;
_closeAdjusted = closeAdjusted;
_volume = volume;
}
}
我需要一个填充了报价列表的 Symbol 实例。
在我的测试中,我想验证我是否可以返回收盘价低于或高于特定值的所有报价。这是我的测试:
[Fact]
public void PriceUnder50()
{
var msftIdentifier = "MSFT";
var quotes = new List<Quote>
{
new Quote(DateTime.Parse("01-01-2009"), 0, 0, 0, 49, 0, 0),
new Quote(DateTime.Parse("01-02-2009"), 0, 0, 0, 51, 0, 0),
new Quote(DateTime.Parse("01-03-2009"), 0, 0, 0, 50, 0, 0),
new Quote(DateTime.Parse("01-04-2009"), 0, 0, 0, 10, 0, 0)
};
_symbol = new Symbol(msftIdentifier, quotes);
var indicator = new UnderPriceIndicator(50);
var actual = indicator.Apply(_symbol);
Assert.Equal(2, actual.Count);
Assert.True(actual.Any(a => a.Date == DateTime.Parse("01-01-2009")));
Assert.True(actual.Any(a => a.Date == DateTime.Parse("01-04-2009")));
Assert.True(actual.Any(a => a.Price == 49));
Assert.True(actual.Any(a => a.Price == 10));
}
好的。
现在,我想用 Autofixture 来做,我是一个真正的初学者。我已经在互联网上阅读了几乎所有关于此工具的内容(作者的博客、codeplex 常见问题解答、github 源代码)。我了解 autofixture 的基本功能,但现在我想在我的实际项目中使用 autofixture。这是我到目前为止所尝试的。
var msftIdentifier = "MSFT";
var quotes = new List<Quote>();
var random = new Random();
fixture.AddManyTo(
quotes,
() => fixture.Build<Quote>().With(a => a.Close, random.Next(1,49)).Create());
quotes.Add(fixture.Build<Quote>().With(a => a.Close, 49).Create());
_symbol = new Symbol(msftIdentifier, quotes);
// I would just assert than 49 is in the list
Assert.True(_symbol.HistoricalQuotes.Contains(new Quote... blabla 49));
理想情况下,我更愿意直接创建 Symbol 的夹具,但我不知道如何自定义我的报价列表。而且我不确定我的测试是否通用,因为在另一个测试中,我需要检查特定值是否高于而不是低于,所以我将复制“夹具代码”并手动添加引号 = 51.
所以我的问题是:
1 - 这是我应该如何使用自动固定装置的方式吗?
2 - 是否可以改进我在示例中使用 autofixture 的方式?