# Mock an APM instance

**URL:** <https://discuss.elastic.co/t/mock-an-apm-instance/224686>\
**Category:** APM\
**Tags:** dotnet\
**Created:** [March 23, 2020, 2:44pm UTC](https://discuss.elastic.co/t/mock-an-apm-instance/224686 "2020-03-23T14:44:44Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![GregKalapos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gregkalapos/32/37205_2.png) [@GregKalapos](https://discuss.elastic.co/u/GregKalapos)\
**Post date:** [March 27, 2020, 9:21pm UTC](https://discuss.elastic.co/t/mock-an-apm-instance/224686/2 "2020-03-27T21:21:12Z")

</div>

Hi @spark,

we actually did not think that `MockPayloadSender` or `MockConfigSnapshot` would be something that our users would use.

I think when you do unit tests, you don't actually need to test the agent.

So, if you have a class where you for example use the public agent API, you can just pass an `ITracer` instance into your code - in production this would come from the real agent and in a unit test you can just pass null.

Like here:

```auto
public void MyMethod(ITracer tracer)
{
	// If ITracer is a real tracer, it'll start the transaction, if ITracer is null, nothing will happen here 
	var transaction = tracer?.StartTransaction("SampleTransaction", "SampleSpan");
	try
	{
		// your code with here
	}
	catch (Exception e)
	{
		transaction?.CaptureException(e);
		throw;
	}
	finally
	{
		transaction?.End();
	}
}

```

In prod. code you can pass `Agent.Tracer` into this method, in a unit test just leave it to `null`. The concept is the same if you unit test a class - you can e.g. dependency inject in the constructor.

---

_[View the full topic](https://discuss.elastic.co/t/mock-an-apm-instance/224686)._
