本文介绍了找不到类型或命名空间名称“PrivateObject"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Visual Studio 2017 并且我试图在 C# 中创建一个私有方法的单元测试(下面的代码):

I am using Visual Studio 2017 and I was trying to create a unit test of a private method in C# (code below):

[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);
    }


}

但是,我收到此错误消息:

However, I got this error message:

错误 CS0246 找不到类型或命名空间名称PrivateObject"

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

I've looked up for articles and tutorials but I still don't know what I am doing wrong.

推荐答案

PrivateObject 和 PrivateType 不适用于面向 netcoreapp2.0 的项目.这里有一个 GitHub 问题:GitHub 问题 366

PrivateObject and PrivateType are not available for projects targeting netcoreapp2.0. There is a GitHub issue here:GitHub Issue 366

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

One option is to inherit from the class and then expose the method in the inherited class.

这篇关于找不到类型或命名空间名称“PrivateObject"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 12:19