c# - 分行覆盖问题
问题描述
我很纠结为什么我在这段代码的测试中没有得到 100% 的测试分支覆盖率:
public List<ReturnItem> FilterItems(List<Items> items)
{
if (items== null || !items.Any())
{
throw new ArgumentException("No items to filter");
}
var newItems = new List<NewItem>();
foreach (var item in items)
{
if (item.Tracking.MidStateDate != null)
{
if (orderLine.Tracking.EndStateDate.GetValueOrDefault() < orderLine.Tracking.MidStateDate)
{
var newItem = new NewItem(item);
newItem.MidStateDate = item.Tracking.MidStateDate.Value;
newItems.Add(newItem);
}
}
}
return newItems;
}
我有以下测试:
- 没有相关产品();
- HasItems_NullTracking();
- HasItems_NoTracking();
- HasItems_HasTracking_NoMidStateDate();
- HasItems_HasTracking_HasMidStateDate_NullEndStateDate();
- HasItems_HasTracking_HasMidStateDate_SmallerEndStateDate();
- HasItems_HasTracking_HasMidStateDate_EndStateDateIsEqual();
- HasItems_HasTracking_HasMidStateDate_LargerEndStateDate();
- HasItems_HasTracking_HasMidStateDate_MixedState();
我无法让分支覆盖率测试达到 100%。这让我觉得我错过了一些东西。我删除了大部分代码,发现问题与这个条件有关if (orderLine.Tracking.EndStateDate.GetValueOrDefault() < orderLine.Tracking.MidStateDate)
。
任何人都可以建议我可以添加的任何其他单元测试来处理分支覆盖问题吗?
解决方案
就像我回复@juharr 一样,我有一个脑电波。
问题在于代码在有问题的条件中的可空日期时间不明确。
if (orderLine.Tracking.EndStateDate.GetValueOrDefault() < orderLine.Tracking.MidStateDate)
^ 导致问题
if (orderLine.Tracking.EndStateDate.GetValueOrDefault() < orderLine.Tracking.MidStateDate.Value)
^ 工作!
推荐阅读
- angular - Ionic 4 需要通过 Playstore 上的应用程序更新
- facebook - 使用分页有没有办法通过 graph api v6.0 获取 facebook 消息?
- swift - 克隆一个 Swift 包来改变它。将本地克隆粘贴到 Xcode 中。无法从此包中的文件快速访问我的项目的类
- node.js - 此表达式不可调用。类型“号码”没有呼叫签名
- git - 如何从 Heroku 克隆存储库
- mysql - 通过 docker 容器的数据库连接问题
- android - 如何实现 Xamarin 语音识别
- c# - 为什么 .NET Core API 中的 POST 参数始终为空
- java - 我正在尝试创建一个活动,如果文件大小小于 16mb,用户可以在其中选择 pdf 和音频文件并上传到 firebase 存储
- reactjs - 在普通函数中使用 react navigation.navigate - (不允许使用挂钩,即 useNavigation)?