samedi 21 mai 2016

Remove anonymous class string from Expression

I've been trying to write a little helper class for my tests that would delay the checking of my object until I call verify then display a list of all the expressions that failed. In my scenario that i'm specifically testing I copy 6 files to a folder, then check that the 6 files were parsed correctly returning 6 objects. While building up my helper class I told it to evaluate 6==5 so that I can see its failing message. Here is the helper class

internal class ObjectVerifier
{
    private readonly Queue<DelayedTests> _delayedTests = new Queue<DelayedTests>();
    public void Verify()
    {
        var exceptions = new StringBuilder();
        while(_delayedTests.Count > 0)
        {
            var delayedTest = _delayedTests.Dequeue();
            if (!delayedTest.ObjectEvaluator.Invoke())
            {
                exceptions.AppendLine($"{delayedTest.TestExpression} failed its evaluation");
            }
        }
        if (exceptions.Length > 0)
        {
            NUnit.Framework.Assert.Fail(exceptions.ToString());
        }
    }

    internal void CheckThat(Expression<Func<bool>> evaluationTest)
    {
        _delayedTests.Enqueue(new DelayedTests
        {
            TestExpression = evaluationTest.ToString(),
            ObjectEvaluator = evaluationTest.Compile()
        });
    }

    private class DelayedTests
    {
        public string TestExpression { get; set; }
        public Func<bool> ObjectEvaluator { get; set; } 
    }
}

and my test that is purposely fails

    [Test]
    public void TestGettingMenuItems()
    {
        CopyFilesToSaveFolder(TestCurseFiles);

        gbl.import_from = ImportSource.Curse;

        List<MenuItem> displayNames;
        List<MenuItem> fileNames;
        ovr017.BuildLoadablePlayersLists(out fileNames, out displayNames);

        _verifier.CheckThat((() => fileNames.Count.Equals(5)));
        _verifier.CheckThat(() => displayNames.Count.Equals(5));
        _verifier.Verify();
    }

The test fails for the correct reason, but the message it leaves is a little rough.

Test Name:  TestGettingMenuItems
Test FullName:  GoldBox.Engine.ovr017Tests.TestGettingMenuItems
Test Source:    C:\Git\coab\GoldBox.Engine.Tests\ovr017Tests.cs : line 34
Test Outcome:   Failed
Test Duration:  0:05:07.258

Result StackTrace:  
at GoldBox.Engine.ObjectVerifier.Verify() in C:\Git\coab\GoldBox.Engine.Tests\ObjectVerifier.cs:line 24
at GoldBox.Engine.ovr017Tests.TestGettingMenuItems() in C:\Git\coab\GoldBox.Engine.Tests\ovr017Tests.cs:line 45
Result Message: 
() => value(GoldBox.Engine.ovr017Tests+<>c__DisplayClass8_0).fileNames.Count.Equals(5) failed its evaluation
() => value(GoldBox.Engine.ovr017Tests+<>c__DisplayClass8_0).displayNames.Count.Equals(5) failed its evaluation

specifically the part value(GoldBox.Engine.ovr017Tests+<>c__DisplayClass8_0).

Is there a way with what I have that I can change it so that it would look more like ()=>fileNames.Count.Equals(5) in the message? I went into the debugger and poked through all the expressions's properties and didn't see any easy way to get what i'm hoping.

Aucun commentaire:

Enregistrer un commentaire