0

Context

This is a unit test scenario.

The methods of the test target class can be called concurrently from different threads, so instead of guarding the logger implementation instance itself with locks, I've chosen to have a thread bound singleton loggers. The methods under test always creating their thread bound loggers via service locator pattern (please do no hijack the question about is this an antipattern or not).

Ninject is programmed as follows in the Arrange part of the test:

 kernel.Bind<ILogger>().To<MyLogger>().InThreadScope();

Question

During the Act part of the test, one or more thread is created by the instance under test (inside).

In the Assert part of the test, I would like to access to the one or more loggers what were created an used by the threads in the class under test, and examine that loggers in the purpose of assertion.

How can I accomplish this task? (access the loggers what was created)

g.pickardou
  • 32,346
  • 36
  • 123
  • 268

1 Answers1

1

Ninject does not offer a specific API for this, however, you can make use of "OnActivation".

Either add it to your existing binding, or use Rebind in the unit test, as follows:

kernel.Rebind<ILogger>().To<MyLogger>().OnActivation(createdInstance => ...do something...);

Replace the "...do something..." with an Action<ILogger> that adds the instance to a (concurrency-safe?) list or similar.

Also see Intercept creation of instances in Ninject for further information.

BatteryBackupUnit
  • 12,934
  • 1
  • 42
  • 68