I have an analytics client that has a track method getting invoked when an event is received.
When I go to mock the class I get this:
Type to mock must be an interface or an abstract or non-sealed class.
Is there any way to spy on and verify parameters passed to a sealed class method?
in JUnit you can more freely do a "spyOn(class.method)..."
How can this be done in C# or with Moq?
Code looks like:
private readonly TelemetryClient _telemetry;
public ClassToTest(TelemetryClient telemetry)
{
_telemetry = telemetry;
}
public async Task ProcessMessageAsync(EventMessage message, CancellationToken cancelToken)
{
try
{
var serviceNotificationEvent = JsonConvert.DeserializeObject<ServiceNotificationEvent>(message.Value);
_telemetry.TrackEvent(AnalyticsConstants.EVENT_RECEIVED);
}
catch (Exception e)
{
_logger.LogError(e, "Error processing event message");
}
}
Current Unit test attempt:
[TestMethod]
public void Test_ReceivedEventAnalytic()
{
Mock<TelemetryClient> _mockTelemetry = new Mock<TelemetryClient>();
_serviceNotificationProcessor.ProcessMessagesAsync(mockMessageList);
IDictionary<String, String> matchingMap = new Dictionary<string, string>();
// build out matchingMap
_mockTelemetry.Verify(t => t.TrackEvent("seamEventReceived", It.IsAny<IDictionary<string, string>>(), null), Times.Once());
}