简体   繁体   English

为什么通用测试会失败,但非通用测试却不会?

[英]Why does a generic test fail but the non generic does not?

I have a bunch of exceptions which follow the same schema: I have a parameter that is given into a text like this:我有一堆遵循相同模式的异常:我有一个参数被赋予这样的文本:

public class ContainerCouldNotStartedException : Exception
{
    private static readonly string Text = "Container could not be started: {0}. Please see docker logging for more information";

    public ContainerCouldNotStartedException()
    {
    
    }
    
    public ContainerCouldNotStartedException(string name)
        : base(String.Format(Text, name))
    {

    }
}

Now I want to test this.现在我想测试一下。

To avoid 10 copied test for 10 exceptions my idea is to test it generic like this:为了避免 10 个异常的 10 个复制测试,我的想法是像这样对其进行通用测试:

public static void Test_String_Is_In_Message(Type type, string name)
{
    ConstructorInfo ctor = type.GetConstructor(new[] { typeof(string) });
    dynamic exception = ctor.Invoke(new object[] { name });
    Assert.That(exception.Message, Does.Contain(name));
}

And call it like this:并这样称呼它:

[Test]
[TestCase("HarryPotter")]
[TestCase("HermineGranger")]
[TestCase("LuciusMalfoy")]
public void Message_Has_Name(string name)
{
    ExceptionTest.Test_String_Is_In_Message(typeof(ContainerCouldNotStartedException), name);
    ExceptionTest.Test_String_Is_In_Message(typeof(DatabaseNameNotCorrectException), name);
    ExceptionTest.Test_String_Is_In_Message(typeof(DatabaseNotCorrectException), name);
    ExceptionTest.Test_String_Is_In_Message(typeof(DatabaseVersionNotReadableException), name);
    ExceptionTest.Test_String_Is_In_Message(typeof(VersionNotFoundException), name);
}

That works very fine and all tests are green.这工作得很好,所有测试都是绿色的。

Now one single exception has a GUID parameter, instead of a string.现在一个异常有一个 GUID 参数,而不是一个字符串。

So instead of having my method Test_String_Is_In_Message copied to times, I made it generic:因此,我没有将我的方法Test_String_Is_In_Message复制到 times,而是使其通用:

public static void Test_String_Is_In_Message<T>(Type type, T name)
{
    ConstructorInfo ctor = type.GetConstructor(new[] { typeof(T) });
    dynamic exception = ctor.Invoke(new object[] { name });
    Assert.That(exception.Message, Does.Contain(name));
}

Now all tests fail.现在所有测试都失败了。

When I debug the test it is all fine for me, I can see the name in the message, but the assert still fails.当我调试测试时,一切正常,我可以在消息中看到名称,但断言仍然失败。 Why?为什么?

The usage of dynamic kind of obfuscated the problem. dynamic类型的使用混淆了问题。 The problem is that Does.Contain() expects a string according to the docs .问题是Does.Contain()根据文档需要一个string So changing your helper method to the following made all the tests pass for me:因此,将您的辅助方法更改为以下内容使我通过了所有测试:

public static void Test_String_Is_In_Message<T>(Type type, T name)
{
    ConstructorInfo ctor = type.GetConstructor(new[] { typeof(T) });
    try
    {
        ctor.Invoke(new object[] { name });
    }
    catch (Exception e)
    {
        Assert.That(e.Message, Does.Contain(name.ToString()));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM