Partial Code Coverage C# - Nunit

情到浓时终转凉″ 提交于 2019-12-23 18:08:02

问题


I have partial code coverage and I don't know why. for people who like the question before they start reading

Want to start by saying "First Post" as well as I am still very Junior in my development career but I have been a relativly quick learner(imo), so here it goes. Using Nunit to test, and MVP based.

Code to be tested -

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.");
        }

    }

The code that is testing the 'if' and the 'else' statements(assuming the repo and view are mocked)-

    [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."));
    }

So my question is, what I am missing in my code. Something more is going on that I need to test and I really would like to find it. I have been heads down in trying to fully understand the in's and out's of testing. Test Driven Development is my goal. If anyone has any kind of input (good or bad) it would be very much appreciated. I wouldn't even mind if someone could throw me juuuuuuuust enough so I can start pulling on that metophorical string that has the answer I am looking for tied to the end of it. I hope I have provided enough information for you all. Thanks!


回答1:


_view is mocked and all method's of _view won't operate on it's arguments, e.g. FillRelatableAccessGroups will receive it's argument, but won't use/execute it.

That is why g.Where(x => x.IsRelatable) and g.Where(x => !x.IsRelatable) are not covered by your tests, because they will never be executed.

If you need full test coverage, consider to use a proper implementation of _view. Something like LINQ: Passing lambda expression as parameter to be executed and returned by method

One thing to keep in mind is that there is no TDD doctrine telling you to achieve full test coverage. A covering 90%+ of the most important spots can be much more valuable.




回答2:


Disclaimer: I am trying to use terms correctly feel free to corrrect me if I use terms incorrectly please I cannot stress the angst I have for "unlearning" something I soaked up incorrectly. I believe I have found my resolution. The current view does not mock the IEnumerable Where method since it is a static method. I am using RhinoMocks and RhinoMocks library is not strong/large enough to handle those system methods (Correct term?). You can create a virtual instance method in another class to wrap the static method inside thus allowing you to finally have the ability to mock the IEnumerable Where method. I located my answers here at this link: Mocking Static methods using Rhino.Mocks



来源:https://stackoverflow.com/questions/18520653/partial-code-coverage-c-sharp-nunit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!