locked
private class variable RRS feed

  • Question

  • Hi - I'm trying to test a method, which sets some data on a private static member of the class under test. If I call this method multiple times, from multiple different tests, it appears that this variable is set during the first test, and remains that way for subsequent tests. How do I clear the "context", so each test method sees an unused object?

    Thanks, Peter

    Tuesday, December 13, 2011 1:56 PM

Answers

  • Thanks for the reply. The static field is used as a simple internal cache. The method I am testing sets and uses this cache - and of course once it's set, the data is just read from there in the future, instead of exercising other pathways in the class under test.

    I've found a solution, by writing a small TestInitialize which uses reflection to set the private static field to null. (And obviously in those cases where I do want to test the use of the cache, I call the method twice, so the cache is set the first time....).

    Tuesday, December 13, 2011 2:39 PM

All replies

  • Hi,

    I fear that this is quite hard. The assemblies are loaded only once so the static value will not be reset between tests.

    You could write a function that resets the static value. This could be private and you could access it through PrivateObject (http://msmvps.com/blogs/deborahk/archive/2009/10/29/unit-testing-exposing-private-members.aspx).

    But this is something that I wouldn't like to have. You add code to a class just to test it! That shouldn't be required.

    So I would check the architecture. Why do you have a state in a static field which causes such problems?

    With kind regards,

    Konrad

    Tuesday, December 13, 2011 2:34 PM
  • Thanks for the reply. The static field is used as a simple internal cache. The method I am testing sets and uses this cache - and of course once it's set, the data is just read from there in the future, instead of exercising other pathways in the class under test.

    I've found a solution, by writing a small TestInitialize which uses reflection to set the private static field to null. (And obviously in those cases where I do want to test the use of the cache, I call the method twice, so the cache is set the first time....).

    Tuesday, December 13, 2011 2:39 PM
  • Thank you for Konrad’s friendly assistance.

     

    Hi xdzgor,

     

    I've found a solution, by writing a small TestInitialize which uses reflection to set the private static field to null. (And obviously in those cases where I do want to test the use of the cache, I call the method twice, so the cache is set the first time....).

     

    Glad to hear that this issue has been resolved, and thank you for sharing your solutions & experience here. It will be very beneficial for other community members who have similar questions.

     

    Have a nice day,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, December 14, 2011 8:49 AM