0

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?

Cœur
  • 34,719
  • 24
  • 185
  • 251
Gobliins
  • 3,553
  • 14
  • 63
  • 109

1 Answers1

2

What about this:

 param0.SetField("b", false);
Hamlet Hakobyan
  • 32,360
  • 6
  • 50
  • 66
  • Except that unit tests **unit tests should never directly access object internals**. This creates a rigid structure and leads to convoluted code. Tests [should exercise objects through their public interface](http://stackoverflow.com/q/5601730/1106367) exclusively. – Adam Jan 06 '13 at 14:12