【问题标题】:NUnit Test override Extension C#NUnit 测试覆盖扩展 C#
【发布时间】:2017-11-30 19:36:36
【问题描述】:

我有以下方法我无法改变

static int A ( string var )
static int A ( this string var )

由于它们被定义为静态的,为了测试它们,我想创建像这样调用它们的公共方法

public static int ATest ( string var )
{
   return A(var);
}
public static int ATestThis ( this string var )
{
   return A(var);
}

然后以这种方式测试它们

namespace test.NUnit
{
    [TestFixture]
    public class myFirstTest
    {
        [Test]
        public void TestOnA() {
            Assert.... // with ATest and ATestThis
        }
    }
}

但是我得到了这两个错误

Type 'Program' already defines a member called A with the same parameter types
The call is ambiguous between the following methods or properties

你有什么想法吗?

【问题讨论】:

  • I have the following methods that I cannot change 你在哪里?同班?
  • 老师提供的
  • 你认为把它们放在两个不同的类中可以解决问题吗?
  • 是的,因为错误文本意味着一个类不能包含两个具有相同名称和签名的方法。因此,您需要重命名其中一个或将它们放到不同的类中。

标签: c# visual-studio unit-testing testing extension-methods


【解决方案1】:

CS0111 类型“程序”已经定义了一个名为“A”的成员,具有相同的参数类型

这个错误告诉你编译器可以区分以下两种方法。

static int A ( string var )
static int A ( this string var )

您可以通过更改其中一个方法的名称或参数或将其中一个方法移动到另一个类来解决此问题。

CS0121 以下方法或属性之间的调用不明确:'Program.A(string)' 和 'Program.A(string)'

这个异常只是第一个的后续错误。当您调用 A(var); 时,编译器不知道您指的是哪个方法,因为它们具有相同的名称和相同的参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 2017-06-01
    • 1970-01-01
    • 2023-04-07
    • 2021-09-06
    相关资源
    最近更新 更多