Search code examples
c#visual-studio-2010unit-testingprivate-members

Private accessor won't access private member


In my class, to test, I have a private boolean instance variable and a method to access it:

MyClass()
{
    private volatile bool b;
    public MyMethod()
    {
         b = false;
    }
}

After creating a unit test for the method

[TestMethod()]
public void MyMethodTest()
{
      PrivateObject param0 = new PrivateObject(new MyClass());
      MyClass_Accessor target = new MyClass_Accessor(param0);
      target.b = false;
}

I get this error:

Property, indexer, or event 'property' is not supported by the language; try directly
calling accessor method 'accessor_taketh' 'accessor_giveth'

but there are no methods like this in the accessor object, instead there is

[Shadowing("b")]
public bool b{ get; set; }

So why do I get the error?


Solution

  • What about this:

     param0.SetField("b", false);