1

I have the following example code as a class variable.

protected static readonly string _url = "SomeURL";

I want to access that _url variable through either stub type or moled type. As I test, public static class variables can be accessed through the stub type. But not the private or protected static class variables. So any idea regarding accessing the private or protected class variables from moles ? It will be very helpful.

Thank you !

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
DineshN
  • 11
  • 1

1 Answers1

0

You can create a stub that derives from the class containing the protected static variable. Use the stub in your test rather than the class you're actually trying to test:

public class MyActualClass
{
  protected static readonly string _url = "SomeURL";
  //... other code
}

public class MyActualClassStub : MyActualClasss
{
  public string GetUrlValue()
  {
    return _url;
  }
}

Something like that. Note that you can't change the value of a readonly member or property though.

Edit: I just realised you asked specifically about how to do this with Moles. I've not used Moles so I can't provide any advice in that direction. However, the approach described above is useful in many situations when you need to "get at" protected members, properties, or methods in a test scenario.

Øyvind
  • 1,600
  • 1
  • 14
  • 33