3

我正在error C3095: 'Xunit::Extensions::InlineDataAttribute': attribute cannot be repeated使用 C++/CLI 代码,但不是 C#。

xUnit.net看起来像是我祈祷的答案 - 一个现代的单元测试框架,带有与 C++/CLI 一起工作的 GUI。但是,使用他们的参数化测试方法给了我错误 C3095,如下所示。

有任何想法吗?

我正在使用最新的 xUnit.net 1.6 和 Visual Studio 2008SP1。

using namespace Xunit;
using namespace Xunit::Extensions;

public ref class ParameterisedTestClass
{
public:

    [Theory]
    [InlineData("Kilroy", 6)]
    // uncomment to cause c3095 [InlineData("Jones", 5)]
    void PropTest(String^ msg, int msgLen)
    {
        Assert::Equal(msg->Length, msgLen);
    }
};

C# 中的等价物很好

using Xunit;
using Xunit.Extensions;

public  class ParameterisedTestClass
{

    [Theory]
    [InlineData("Kilroy", 6)]
    [InlineData("Jones", 5)]
    public void PropTest(String msg, int msgLen)
    {
        Assert.Equal(msg.Length, msgLen);
    }
};
4

3 回答 3

7

我的猜测是这是由于继承,而其中一个编译器弄错了。

InlineDataAttribute继承自DataAttribute。现在DataAttribute声明允许多个实例:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]

InlineDataAttribute本身没有任何显式AttributeUsage属性。我怀疑C++ 编译器没有发现继承的 AllowMultiple... 或者它可能不应该被继承。我找不到任何关于AttributeUsageAttribute自身继承的详细文档 - 虽然它有Inherited=true,所以我想它应该完全由InlineDataAttribute......继承

于 2010-06-25T09:30:14.403 回答
6

嗯...我在这里这里查看了定义,并在下面复制了它们(删减);AllowMultiplevia的继承DataAttribute在 C# 中工作正常:

class Test
{
    [InlineData]
    [InlineData]
    static void Main() { }
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
class DataAttribute : Attribute {}

class InlineDataAttribute : DataAttribute { }

因此,如果它不适用于 C++/CLI,我猜 C++/CLI 根本没有处理隐含的[AttributeUsage]. 您应该向 Xunit 提出请求,要求他们[AttributeUsage]InlineDataAttribute.

于 2010-06-25T09:35:09.253 回答
1

我使用这个 C++/CLI 片段进行复制:

[AttributeUsage(AttributeTargets::All, AllowMultiple = true, Inherited = true)]
ref class BaseAttribute : Attribute {};

ref class DerivedAttribute : BaseAttribute {};

[Derived]
[Derived]    // Error C3095
ref class Test {};

显然,这是 C++/CLI 编译器中的一个错误。我没有在 connect.microsoft.com 上看到它的报告。自己这样做可能不值得,语言处于维护模式。

可能的解决方法是编辑 xUnit 的源代码以再次分配 AllowMultiple 或创建您自己的继承 InlineDataAttribute 的 InlineDataFixedAttribute。

于 2010-06-25T11:16:19.303 回答