我正在尝试编写一个单元测试来测试一个方法,该方法基本上接受一些数据组,然后运行一个方法。由于某种原因,我的设置从未被调用。我已经调试过了,我已经检查了传入和传出的类型和数据,分组后完全一样。任何想法为什么这不起作用?
这是在我的测试中:
MyArray[] grouped = myArray
.SelectMany(x => x.AccountValues)
.GroupBy(x => x.TimeStamp)
.Select(g2 =>
new AccountValue {
Amount = g2.Sum(x => x.Amount),
TimeStamp = g2.Key })
.ToArray();
helper
.Setup(s => s.Compute(grouped, grouped.Count())
.Returns(someValue);
var result = _engine.Get(accountNumbers, startDate, endDate, code);
helper.Verify(v => v.Compute(grouped, grouped.Count()), Times.Exactly(1));
我正在测试的实际方法如下:
public decimal? Get(long[] accountNumbers,
DateTime startDate,
DateTime endDate,
long code)
{
var accountNumbersInt = Array.ConvertAll(accountNumbers, i => (int)i);
var myArray = TransactionManager
.Get(accountNumbersInt, startDate, endDate, code);
var accountValues = GroupData(myArray);
var result= Helper.Compute(accountValues, accountValues.Count());
return result;
}
internal myArray[] GroupData(Account[] myArray)
{
var grouped = myArray
.SelectMany(x => x.AccountValues)
.GroupBy(x => x.TimeStamp)
.Select(g2 =>
new AccountValue {
Amount = g2.Sum(x => x.Amount),
TimeStamp = g2.Key })
.ToArray();
return grouped;
}
编辑:助手在测试设置中设置如下
[SetUp]
public void Setup()
{
_engine = new CalcEngine();
helper = new Mock<IHelper>();
_engine.Helper = helper.Object;
}