【问题标题】:Unit test setup never invoked从未调用单元测试设置
【发布时间】:2013-07-22 06:16:49
【问题描述】:

我正在尝试编写一个单元测试来测试一个方法,该方法基本上接受一些数据组,然后运行一个方法。由于某种原因,我的设置从未被调用。我已经调试过了,我检查了传入和传出的类型和数据,分组后完全一样。任何想法为什么这不起作用?

这是在我的测试中:

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;
}

【问题讨论】:

  • 什么是助手?通常它会在执行实际测试时运行,当您在调试时第一次跨过该特定行时它不应该改变。
  • 你是如何模拟助手的?这是重要的信息,因为我们无法查看您是否真的在调用助手或助手模拟 - 也许这是错误
  • @Stephan - 使用帮助代码编辑帖子
  • 你有没有试过验证这个:helper.Verify(v =&gt; v.Compute(It.IsAny&lt;MyArray[]&gt;(), It.IsAny&lt;int&gt;()), Times.Exactly(1)); -> 如果验证了,那么你知道模拟被调用了,但是参数被断言错误,如果没有,那么模拟永远不会被调用
  • 是的,可以,但就我所见,参数设置正确

标签: linq unit-testing moq verify


【解决方案1】:

这里的这部分,不会调用你的方法:

helper
   .Setup(s => s.Compute(grouped, grouped.Count())
   .Returns(someValue);

您的测试编写方式,我假设您正在测试您的_engine.Get() 方法。 这个想法是,您将创建一个模拟,将其传递给您正在测试的方法(执行此操作的不同方法)调用您正在测试的方法,然后观察结果调用的其他内容(副作用)。

我看到在您的 Get() 方法中,您正在执行以下操作:

var result= Helper.Compute(accountValues, accountValues.Count());

假设Helper 与您尝试验证您需要从单元测试中通过它相同,因此类似于:

helper
   .Setup(s => s.Compute(grouped, grouped.Count())
   .Returns(someValue);

_engine.Helper = helper.Object;

// your verifications here ...

【讨论】:

  • helper 就是这么设置的,所以才这么诡异
  • @newbie_86 在我看来,您需要模拟您的 TransactioManager 并将其设置为返回您在单元测试中创建的数组。目前验证会失败,因为您没有使用相同的对象(相同的结果集)调用方法
猜你喜欢
  • 2015-03-05
  • 1970-01-01
  • 2021-09-13
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 2017-08-31
  • 2016-08-19
相关资源
最近更新 更多