'Disable or Mock a background service in xunit .net core

I have this line of code in my startup.cs in my project

services.AddHostedService<AkkaService>(sp => (AkkaService)sp.GetRequiredService<IEasyProcessor>());

I need to disable this line when I run my tests .I don't want use environment variable .I just need to mock or disable this service . I Mock this service in my test solution like this :

    Mock<IEasyProcessor> easyprocessor= new Mock<IEasyProcessor>();

    services.AddScoped<IEasyProcessor>(processor =>{ return easyprocessor.Object; });

But I get this error :

System.InvalidCastException: 'Unable to cast object of type 'Castle.Proxies.IEasyProcessorProxy' to type 'easy.api.Akka.AkkaService'.'


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source