michael
michael

Reputation: 15282

C# Unit Testing: How do I set a Lazy<T>.ValueCreated to false?

Basically, I have a unit test that gets a singleton instance of a class. Some of my tests required me to mock this singleton, so when I do Foo.Instance I get a different type of instance.

The problem is that my checks are passing individually, but failing overall because one test is interfering with another.

I tried to do a TestCleanup where I set: Foo_Accessor._instance = null; but that didn't work. What I really need is Foo_Accessor._instance.IsValueCreated = false; (_instance is a Lazy).

Any way to unset the Lazy object that I didn't think of?

Upvotes: 1

Views: 1497

Answers (1)

myermian
myermian

Reputation: 32515

Why not do: Foo_Accessor._instance = new Lazy<Foo>(Something);

Make sure the "Something" is exactly the same as what your Foo class is doing.

Upvotes: 5

Related Questions