Marc Vitalis
Marc Vitalis

Reputation: 2249

Unit Testing Code with WeakReference

I didn't create the framework but I need to write test for it. The scenario...

When executing a method that relies on the registered object, my test sometimes fails because it has been garbage collected before i performed test to it.

Can I do something to prevent GC from collecting my WeakReference object?

NOTE: No changes should be done in the framework (BaseClass), only in my test class

Upvotes: 1

Views: 922

Answers (1)

Marc Vitalis
Marc Vitalis

Reputation: 2249

Sorry, I found an answer to this... During testing you should make use of...

GC.KeepAlive(weakObject)

Full reference here.

http://defragdev.com/blog/?p=129

Upvotes: 1

Related Questions