0

I have to the following protected method in my color class and i can't access it in my MSTest Test Project(.NET Core) class how can i get access to this method?

I am using Visual Studio 2019, and I can't change any of the class definitions.

I have tried using PrivateObject and that didn't work....

"The type or namespace 'PrivateObject' could not be found (are you missing a using directive or an assembly reference?

public class Color : Shape {
    protected override IEnumerable<string> GetSizes(string name){
        //do stuff    
    }
}
Bizhan
  • 16,157
  • 9
  • 63
  • 101
  • You could test a method that's _not_ protected but calls the protected one, since unit tests aren't supposed to dig into the implementation, only verify the _public_ behavior. – D Stanley Feb 05 '20 at 20:30
  • 1
    OP, you said you tried `PrivateObject`. Can you show us what you tried? –  Feb 05 '20 at 20:30
  • my best piece of advice is to redesign your classes in such way that you only need to test the public API of every class. if you didn't wanted to make a method public you can still test the protected internal methods. – Bizhan Feb 05 '20 at 20:36
  • OP, please refrain from duplicating your questions. If you edit your previous question and comment why the proposed solution wasn't what you needed, that question will be moved to the top of the question queue and will probably show up on the front page, earning it a fresh look from more programmers. –  Feb 05 '20 at 20:40
  • I tried PrivateObject color = new PrivateObject(new Color()); but it says "The type or namespace 'PrivateObject' could not be found (are you missing a using directive or an assembly reference?) –  Feb 05 '20 at 20:47
  • @Golfer1145 Yes, I see that in your previous question, which has an answer. –  Feb 05 '20 at 20:53
  • Those answers also didn't work –  Feb 05 '20 at 21:04
  • @Golfer1145 Then write a comment on the answer explaining why. Or edit the question like I suggested. Duplicating your question is not the appropriate solution here. They took time out of their very important day to help you, and you didn't provide them *any* feedback to help guide each other to a working solution? –  Feb 05 '20 at 21:05
  • here you can find the namespace of PrivateObject. or you could have just pressed `ctrl+.` in your visual studio https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.testtools.unittesting.privateobject?view=mstest-net-1.2.0 – Bizhan Feb 05 '20 at 21:15
  • @Bizhan That class doesn't exist in .Net Core. https://github.com/Microsoft/testfx/issues/366 –  Feb 05 '20 at 21:22
  • i didn't notice net core in your question, here is the closed issue https://github.com/Microsoft/testfx/issues/366 – Bizhan Feb 05 '20 at 21:25
  • Just make the method `protected internal override ...` and friend your test assembly by adding [assembly: InternalsVisibleTo("Your.Test.Assembly")] into the project's `AssemblyInfo.cs` where your class is declared. – MikeLimaSierra Feb 06 '20 at 09:13

0 Answers0