3

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!

4

2 に答える 2

1

免責事項:私は用語を正しく使用しようとしていますが、用語を間違って使用している場合は自由に修正してください。私は自分の解決策を見つけたと信じています。現在のビューは静的メソッドであるため、IEnumerable Where メソッドをモックしません。RhinoMocks を使用していますが、RhinoMocks ライブラリは、これらのシステム メソッドを処理するのに十分な強度/大きさがありません (正しい用語ですか?)。別のクラスで仮想インスタンス メソッドを作成して静的メソッドをラップすることができるため、最終的に IEnumerable Where メソッドをモックすることができます。私はこのリンクで私の答えを見つけました: Rhino.Mocks を使用した静的メソッドのモッキング

于 2013-09-03T17:09:54.603 に答える
1

_viewは嘲笑され、_view のすべてのメソッドはその引数に対して動作しません。たとえばFillRelatableAccessGroups、その引数を受け取りますが、それを使用/実行しません。

g.Where(x => x.IsRelatable)g.Where(x => !x.IsRelatable)は決して実行されないため、テストの対象外です。

完全なテスト カバレッジが必要な場合は、 の適切な実装を使用することを検討してください_view。LINQのようなもの: メソッドによって実行されて返されるパラメーターとしてラムダ式を渡す

心に留めておくべきことの 1 つは、完全なテスト カバレッジを達成するように指示する TDD の教義はないということです。最も重要なスポットの 90% 以上をカバーすることは、はるかに価値があります。

于 2013-08-29T22:21:20.083 に答える