'The type or namespace name 'PrivateObject' could not be found
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:
Error CS0246 The type or namespace name 'PrivateObject' could not be found
I've looked up for articles and tutorials but I still don't know what I am doing wrong.
Solution 1:[1]
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.
Solution 2:[2]
As @Tim mention it is not included on .net core https://github.com/Microsoft/testfx/issues/366 but if you follow the thread you can find a reference to https://gist.github.com/skalinets/1c4e5dbb4e86bd72bf491675901de5ad Which contains a "Poor man Private method implementation" for completeness the code is copied below
public class PrivateObject
{
private readonly object o;
public PrivateObject(object o)
{
this.o = o;
}
public object Invoke(string methodName, params object[] args)
{
var methodInfo = o.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance);
if (methodInfo == null)
{
throw new Exception($"Method'{methodName}' not found is class '{o.GetType()}'");
}
return methodInfo.Invoke(o, args);
}
}
I have tried it on a few unit tests and worked just fine for me, I will update if I find issues.
Solution 3:[3]
if you hover over PrivateObject( and right click.. is there an option for go to definition?
that command will tell you what it thinks that class is..
if you cannot go to definition via the context menu, then it means you're missing a reference and the compiler doesn't know what PrivateObject is..
If that is the case, you can add a reference to it via your NuGet Package Manager or by right clicking the project and going to Add Reference.
Also remember to add the Namespace to the top of the file with a using statement, once those are all in place it should know what that object is. You should not get any errors at that point, and your Go to Definition context menu should take you to an actual class definition.
Solution 4:[4]
While PrivateObject shares the same Microsoft.VisualStudio.TestTools.UnitTesting namespace as TestMethodAttribute and related attribues, it belong to a separate assembly
Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll
Check to make sure that the assembly is referenced by the test project.
You also need to cast the result because Invoke returns an object
string result = (string)privateObject.Invoke("HelloTest");
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tim |
| Solution 2 | J.J |
| Solution 3 | JBoothUA |
| Solution 4 | Nkosi |
