c# - 找不到类型或命名空间名称 'PrivateObject'

标签 c# unit-testing

我正在使用 Visual Studio 2017,我试图在 C# 中创建私有(private)方法的单元测试(代码如下):

[TestClass]
public class CalculatorTests
{
    [TestMethod]
    public void TestCalculator_Hello()
    {
        var calc = new Calculator(1);

        var privateObject = new PrivateObject(calc);

        string expected = "hello!";

        string result = privateObject.Invoke("HelloTest");
        Assert.AreEqual(expected, result);
    }


}

但是,我收到了这个错误信息:

Error CS0246 The type or namespace name 'PrivateObject' could not be found

我已经查找了文章和教程,但我仍然不知道我做错了什么。

最佳答案

PrivateObject 和 PrivateType 不适用于针对 netcoreapp2.0 的项目。这里有一个 GitHub 问题: GitHub Issue 366

一种选择是继承类,然后在继承类中公开方法。

关于c# - 找不到类型或命名空间名称 'PrivateObject',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53273228/

相关文章:

c# - 等到进程结束

c# - 应用程序可以在手动运行时保存文件,但在由任务计划程序运行时不能

c# - 站点地图/面包屑未在默认页面上显示 Url 路由

unit-testing - 测试 - 生成不符合架构的 xml 文件

json - 如何为 json.Marshal 的失败案例编写单元测试?

C# - 为什么这个变量在通过方法后没有被改变

c# - Newtonsoft 中的 "Type specified in JSON is not compatible"用于出现在不同加载程序集中的类

unit-testing - RxJava2 : TestSubscriber. AssertNoErrors 总是通过

linux - 是否可以仅使用控制台而不使用 UI 来使用 wxWidgets 运行单元测试?

angular - 对话框测试 - 当我模拟 this.store$.pipe( select(...) ...).subscribe(..) 时,Jasmine 返回 this.store$.pipe 不是函数