我有部分代码覆盖率,但我不知道为什么。对于在开始阅读之前喜欢这个问题的人
首先想说“第一篇文章”,我的开发生涯还很初级,但我的学习速度相对较快(我认为),所以就这样吧。使用Junit进行测试,并基于MVP。
待测试代码 -
void _view_Delete(object sender, EventArgs<Guid> e)
{
_agRepo.Delete(_agRepo.GetByID(e.Value));
var g = _agRepo.GetAll();
if (g.Count() > 0)
{
_view.FillRelatableAccessGroups(g.Where(x => x.IsRelatable));//partial coverage
_view.FillStandAloneAccessGroups(g.Where(x => !x.IsRelatable));//partial coverage
}
else
{
_view.ShowErrorMsg(true, "No Access Groups Found.");
}
}
测试“if”和“else”语句的代码(假设存储库和视图被模拟)-
[Test]
public void TestDelete()
{
_view.Raise(v => v.Delete += null, this, new EventArgs<Guid>(1.ToGuid()));
_agRepo.AssertWasCalled(r => r.Delete(_agRepo.GetByID(1.ToGuid())));
_view.AssertWasCalled(v => v.FillRelatableAccessGroups(Arg<IEnumerable<AccessGroup>>.Is.Anything));
_view.AssertWasCalled(v => v.FillStandAloneAccessGroups(Arg<IEnumerable<AccessGroup>>.Is.Anything));
}
[Test]
public void TestDeleteNoGroups()
{
_agList.Clear();
_view.Raise(v => v.Delete += null, this, new EventArgs<Guid>(1.ToGuid()));
_agRepo.AssertWasCalled(r => r.Delete(_agRepo.GetByID(1.ToGuid())));
_view.AssertWasNotCalled(v => v.FillRelatableAccessGroups(Arg<IEnumerable<AccessGroup>>.Is.Anything));
_view.AssertWasNotCalled(v => v.FillStandAloneAccessGroups(Arg<IEnumerable<AccessGroup>>.Is.Anything));
_view.AssertWasCalled(x => x.ShowErrorMsg(true, "No Access Groups Found."));
}
所以我的问题是,我的代码中缺少什么。还有更多事情需要我测试,我真的很想找到它。我一直在埋头尝试完全理解测试的细节。测试驱动开发是我的目标。如果有人有任何意见(好的或坏的),我们将不胜感激。我什至不介意是否有人能给我足够多的东西,这样我就可以开始拉那根隐喻的绳子,它的末端绑着我正在寻找的答案。我希望我已经为大家提供了足够的信息。谢谢!