单元测试异常属性



I have exception

class SyntaxError : Exception {
    public SyntaxError(int l) {
        line = l;
    }
    public int line;
}

我正在使用单元测试来测试类解析器,该解析器在特定输入上应该抛出上面的异常。我正在使用这样的代码:

    [TestMethod]
    [ExpectedException(typeof(Parser.SyntaxError))]
    public void eolSyntaxError()
    {
        parser.reader = new StringReader("; alfan; betannnna");
        parser.eol();
    }

有没有聪明的简单方法来检查SyntaxError.line == 1

我想出的最好的是:

    [TestMethod]
    public void eolSyntaxError()
    {
        try {
            parser.reader = new StringReader("; alfan; betannnna");
            parser.eol();
            Assert.Fail();
        } catch (SyntaxError e) {
            Assert.AreEqual(1, e.line);
        }
    }

我不太喜欢它,有更好的方法吗?

考虑使用 FluentAssertions。然后,您的测试将如下所示:

[TestMethod]
public void eolSyntaxError()
{
    parser.reader = new StringReader("; alfan; betannnna");
    Action parseEol = () => parser.eol();
    parseEol
        .ShouldThrow<SyntaxError>()
        .And.line.Should().Be(1);
}

否则,你的方法几乎和它得到的一样好。

您可以编写类似于 NUnit 中的方法

public T Throws<T>(Action code) where T : Exception
{
    Exception coughtException = null;
    try
    {
        code();
    }
    catch (Exception ex)
    {
        coughtException = ex;
    }
    Assert.IsNotNull(coughtException, "Test code didn't throw exception");
    Assert.AreEqual(coughtException.GetType(), typeof(T), "Test code didn't throw same type exception");
    return (T)coughtException;
}

然后您可以在测试方法中使用它

Parser.SyntaxError exception = Throws<Parser.SyntaxError>(() => parser.eol());
Assert.AreEqual(1, exception.line);

根据我的评论,如果您遇到语法错误的行是相关的,请将其包含在自定义异常类中,如下所示。

public class SyntaxError : Exception
{
     public SyntaxError(int atLine)
     {
         AtLine = atLine;
     }
     public int AtLine { get; private set; }
}

然后很容易测试。

编辑 - 阅读问题(!)后,这里有一个简单的附加断言方法,它将整理您的异常断言。

public static class xAssert
{
    public static TException Throws<TException>(Action a) where TException : Exception
    {
        try
        {
            a();
        }
        catch (Exception ex)
        {
            var throws = ex as TException;
            if (throws != null)
                return throws;
        }
        Assert.Fail();
        return default(TException);
    }
}

用法如下...

public class Subject
{
    public void ThrowMyException(int someState)
    {
        throw new MyException(someState);
    }
    public void ThrowSomeOtherException()
    {
        throw new InvalidOperationException();
    }
}
public class MyException : Exception
{
    public int SomeState { get; private set; }
    public MyException(int someState)
    {
        SomeState = someState;
    }
}
[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
        var subject = new Subject();
        var exceptionThrown = xAssert.Throws<MyException>(() => { subject.ThrowMyException(123); });
        Assert.AreEqual(123, exceptionThrown.SomeState);
    }
}
我不知道对此

有开箱即用的解决方案,但我已经看到了这样的期望概念:

[TestMethod]
public void EolSyntaxError()
{
    Expectations.Expect<(SyntaxError>(
        () =>
        {
            parser.reader = new StringReader("; alfan; betannnna");
            parser.eol();
        },
        e =>
        {
            Assert.AreEqual(1, e.line);
        });
}

期望需要实现。我认为会有图书馆已经这样做了。无论如何,Expectations中的Expect方法可能如下所示:

public static void Expect<TExpectedException>(
    System.Action action,
    System.Action<TExpectedException> assertion) where TExpectedException : Exception
{
    if (action == null) { throw new ArgumentNullException("action"); }
    try
    {
        action.Invoke();
        Assert.Fail(string.Format("{0} expected to be thrown", typeof(TExpectedException).Name));
    }
    catch (TExpectedException e)
    {
        assertion.Invoke(e);
    }
}

最新更新