I am trying to test masstransit consumer
my consumer look like this:
public class FilePolicyEvaluationConsumer : IFilePolicyEvaluationConsumer
{
public readonly IFilePolicyEvaluationCore _filePolicyEvaluationCore;
public readonly IRepositoryClient _repositoryClient;
public readonly IFPRepository _fPRepository;
public FilePolicyEvaluationConsumer()
{
}
public FilePolicyEvaluationConsumer(IFilePolicyEvaluationCore filePolicyEvaluationCore, IRepositoryClient repositoryClient, IFPRepository fPRepository)
{
_filePolicyEvaluationCore = filePolicyEvaluationCore;
_repositoryClient = repositoryClient;
_fPRepository = fPRepository;
}
public Task Consume(ConsumeContext<InvokePolicyOnFileMessage> context)
{
return Task.Run( async () => {
Guid fileId = context.Message.FileId;
FileMetadata fileMetaData = await _repositoryClient.GetFileMetadata(fileId);
_filePolicyEvaluationCore.Run(fileMetaData, context.Message.CorrelationId);
});
}
but when i configure the test like this
_harness = new InMemoryTestHarness();
FilePolicyEvaluationConsumer filePolicy = new FilePolicyEvaluationConsumer();
_consumer = _harness.Consumer<FilePolicyEvaluationConsumer>();
await _harness.Start();
await _harness.InputQueueSendEndpoint.Send(message1);
when i enter in the test to consumer all objects _filePolicyEvaluationCore,_repositoryClient,_fPRepository in the consumer are null, how can i inject to this objects?
It is null because you probably use a container in your production code and inject the constructor parameter. MassTransit has no injection built-in since it does not use any containers.
You can use the consumer factory overload to make it work:
_harness = new InMemoryTestHarness();
var filePolicy = new FilePolicyEvaluationConsumer();
_consumer = _harness.Consumer(
() => new FilePolicyEvaluationConsumer(filePolicy));
Related
I am using ASP.NET Core, and I am adding some users to a collection via SingalR hub endpoint:
public class MatchMakingHub : Hub
{
//....
// called by client
public async Task EnlistMatchMaking(int timeControlMs)
{
Guid currentId = Guid.Parse(this.Context.User.GetSubjectId());
GetPlayerByIdQuery getPlayerByIdQuery = new GetPlayerByIdQuery(currentId);
Player currentPlayer = await requestSender.Send<Player>(getPlayerByIdQuery);
var waitingPlayer = new WaitingPlayer(currentPlayer, timeControlMs);
this.matchMakePool.Add(waitingPlayer);
}
}
matchMakePool being a singleton collection.
Later, I have an ASP.NET Core background service fetch the users from the collection, and notify them about being fetched:
public class MatchMakingBackgroundService : BackgroundService
{
private readonly MatchMakePoolSingleton matchMakePoolSingleton;
private readonly IServiceProvider serviceProvider;
private const int RefreshTimeMs = 1000;
public MatchMakingBackgroundService(MatchMakePoolSingleton matchMakePoolSingleton, IServiceProvider serviceProvider)
{
this.matchMakePoolSingleton = matchMakePoolSingleton;
this.serviceProvider = serviceProvider;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while(!stoppingToken.IsCancellationRequested)
{
var result = matchMakePoolSingleton.RefreshMatches();
var tasks = new List<Task>();
foreach(var match in result)
{
tasks.Add(StartGameAsync(match));
}
await Task.WhenAll(tasks);
await Task.Delay(RefreshTimeMs, stoppingToken);
}
}
private async Task StartGameAsync(MatchMakeResult match)
{
using var scope = serviceProvider.CreateScope();
var sender = scope.ServiceProvider.GetRequiredService<ISender>();
var hubContext = serviceProvider.GetRequiredService<IHubContext<MatchMakingHub>>();
CreateNewGameCommand newGameCommand = new CreateNewGameCommand(match.WhitePlayer.Id, match.BlackPlayer.Id, TimeSpan.FromMilliseconds(match.TimeControlMs));
Guid gameGuid = await sender.Send(newGameCommand);
await hubContext.Clients.User(match.WhitePlayer.Id.ToString()).SendAsync("NotifyGameFound", gameGuid);
await hubContext.Clients.User(match.BlackPlayer.Id.ToString()).SendAsync("NotifyGameFound", gameGuid);
}
}
My problem is that NotifyGameFound is not being called in the client side. When I notified them straight from the hub itself it was received, but for some reason it doesn't when I call it through the provided IHubContext<MatchMakingHub>. I suspect that this is because it runs on another thread.
Here is the client code:
// in blazor
protected override async Task OnInitializedAsync()
{
var tokenResult = await TokenProvider.RequestAccessToken();
if(tokenResult.TryGetToken(out var token))
{
hubConnection
= new HubConnectionBuilder().WithUrl(NavigationManager.ToAbsoluteUri("/hubs/MatchMaker"), options =>
{
options.AccessTokenProvider = () => Task.FromResult(token.Value);
}).Build();
await hubConnection.StartAsync();
hubConnection.On<Guid>("NotifyGameFound", id =>
{
//do stuff
});
await MatchMakeRequast();
}
}
async Task MatchMakeRequast() =>
await hubConnection.SendAsync("EnlistMatchMaking", Secs * 1000);
I use injection to achieve this.
In my servers Startup.cs ConfigureServices mothod I have:
services.AddScoped<INotificationsBroker, NotificationsBroker>();
In your case I am assuming you are injecting MatchMakingBackgroundService
Something like:
services.AddScoped<MatchMakingBackgroundService>();
In my NotificationsBroker constructor I inject the context:
private readonly IHubContext<NotificationsHub> hub;
public NotificationsBroker(IHubContext<NotificationsHub> hub)
=> this.hub = hub;
I then inject the broker into any service I require it and the service can call the hubs methods I expose via the interface.
You don't have to go the extra step, I do this for testing, you could inject the context directly into your MatchMakingBackgroundService.
I am using MassTransit 5.5.5 version and xunit 2.4.1
My consumer looks like this
public class StorageInformationConsumer : IConsumer<CreateStorageInformationSummary>
{
private readonly IBus _serviceBus;
private readonly USIntegrationQueueServiceContext _USIntegrationQueueServiceContext;
public StorageInformationConsumer(IBus serviceBus, USIntegrationQueueServiceContext USIntegrationQueueServiceContext)
{
_serviceBus = serviceBus;
_USIntegrationQueueServiceContext = USIntegrationQueueServiceContext;
}
public async Task Consume(ConsumeContext<CreateStorageInformationSummary> createStorageInformationSummarycontext)
{
//....
}
}
And my Test like this
public class StorageInformationConsumerTest
{
private readonly USIntegrationQueueServiceContext _dbContext;
private readonly Mock<IBus> _serviceBusMock;
private readonly StorageInformationConsumer _storageInformationConsumer;
public StorageInformationConsumerTest()
{
var options = new DbContextOptionsBuilder<USIntegrationQueueServiceContext>()
.UseInMemoryDatabase(databaseName: "InMemoryArticleDatabase")
.Options;
_dbContext = new USIntegrationQueueServiceContext(options);
_serviceBusMock = new Mock<IBus>();
_storageInformationConsumer = new StorageInformationConsumer(_serviceBusMock.Object, _dbContext);
}
[Fact]
public async void ItShouldCreateStorageInformation()
{
var createStorageInformationSummary = new CreateStorageInformationSummary
{
ClaimNumber = "C-1234",
WorkQueueItemId = 1,
StorageInformation = CreateStorageInformation(),
};
//How to consume
}
}
How to consume the CreateStorageInformationSummary message in order to call consumer, following doesn't work
var mockMessage = new Mock<ConsumeContext<CreateStorageInformationSummary>>(createStorageInformationSummary);
await _storageInformationConsumer.Consume(mockMessage.Object);
Since you have not clarified what is actually not working, the most I can provide is how to create the mock context and pass it to the subject method under test.
This is simple enough since ConsumeContext<T> is already an interface
[Fact]
public async Task ItShouldCreateStorageInformation() {
//Arrange
var createStorageInformationSummary = new CreateStorageInformationSummary {
ClaimNumber = "C-1234",
WorkQueueItemId = 1,
StorageInformation = CreateStorageInformation(),
};
//Mock the context
var context = Mock.Of<ConsumeContext<CreateStorageInformationSummary>>(_ =>
_.Message == createStorageInformationSummary);
//Act
await _storageInformationConsumer.Consume(context);
//Assert
//...assert the expected behavior
}
Also take note that the test has been updated to return async Task and not async void
Reference Moq Quickstart
I'm a little confused about how to avoid service locator when using a console application
Program
public static int Main(string[] args)
{
// Configuration
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").AddEnvironmentVariables().Build();
// DI container
var services = new ServiceCollection();
ConfigureServices(services, configuration);
var serviceProvider = services.BuildServiceProvider();
// Do I pass along the serviceProvider?
// Can resolve using locator pattern do I just use this in my classes?
// var exampleRepository = _serviceProvider.GetService<IExampleRepository>();
// Execute the correct command based on args
return CommandLineOptions.Execute(args);
}
private static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddScoped<ApplicationDbContext>((s) => new ApplicationDbContext(configuration.GetSection("Data:DefaultConnection:ConnectionString").Value));
services.AddScoped<IExampleRepository, ExampleRepository>();
}
CommandLineOptions
public static class CommandLineOptions
{
public static int Execute(string[] args, IServiceProvider serviceProvider)
{
try
{
var app = new CommandLineApplication
{
Name = "dnx abc",
FullName = "Abc Commands",
Description = "ABC",
};
app.VersionOption("--version", PlatformServices.Default.Application.ApplicationVersion);
app.HelpOption("-?|-h|--help");
app.OnExecute(() =>
{
//ShowLogo();
app.ShowHelp();
return 2;
});
app.Command(
"task",
task=>
{
task.Name = "Task1";
task.FullName = "Task1";
task.Description = "Tasks";
task.HelpOption("-?|-h|--help");
task.OnExecute(() => { task.ShowHelp(); return 0; });
task.Command(
"task1",
data =>
{
data.FullName = "Task1 command";
data.Description = "Task1";
data.OnExecute(() =>
{
// Need to inject
var p = new Task1();
p.Process()
return 0;
});
I need to inject the IExampleRepository into the new Task1()
Task1
public class Task1
{
public Task1()
{
}
private readonly IExampleRepository _exampleRepository;
public Task1(IExampleRepository exampleRepository)
{
_exampleRepository = exampleRepository;
}
public void Process() {
....
}
So basically my understanding is that I register my dependencies, then I should be able to inject them throughout my classes. I'm not sure if I need to pass my serviceProvider down?
I believe that in MVC there is magic that happens to accomplish this. How would I go about injecting without using the service locator pattern?
Basically you don't want to have to pass IServiceProvider to any class except the bootstrapper (Startup) or factory methods/classes as this ties your classes to the specific IoC container.
What you can do is add dependencies to your CommandLineApplication class and resolve it in the Main method and from here you can start your dependency injection chain. This will work as long as you need/want to resolve all of your dependencies at once.
When you get in an situation where you only need to load a subset of it (i.e. using a different service or program logic when a certain parameter is passed), you'll need a kind of factory (a factory is a thin wrapper that creates and configures an object before passing it, in case of IoC it also resolves the dependencies).
In the factory implementation it's okay to reference the container, if necessary (you need scoped dependencies or transient resolving per object creation). You'll also need a factory if you need more than one instance of Task1.
There are two ways. For very simple factories you can use a factory method, that can be directly used while doing your IServiceCollection registrations.
services.AddTransient<Task1>();
services.AddTransient<Func<Task1>>( (serviceProvider) => {
return () => serviceProvider.GetService<Task1>();
});
then inject into your dependency.
public class MyTaskApplication
{
private readonly Func<Task> taskFactory;
public MyApplicationService(Func<Task> taskFactory)
{
this.taskFactory = taskFactory;
}
public void Run()
{
var task1 = taskFactory(); // one instance
var task2 = taskFactory(); // another instance, because its registered as Transient
}
}
If you need more complex configuration or with runtime parameter, it may make more sense to make a factory class.
public class TaskFactory : ITaskFactory
{
private readonly IServiceProvider services;
public TaskFactory(IServiceProvider services)
{
this.services = services;
}
public Task1 CreateNewTask()
{
// get default task service, which is transient as before
// so you get a new instance per call
return services.GetService<Task1>();
}
public Task1 CreateNewTask(string connectionString)
{
// i.e. when having multiple tenants and you want to
// to the task on a database which is only determined at
// runtime. connectionString is not know at compile time because
// the user may choose which one he wants to process
var dbContext = MyDbContext(connectionString);
var repository = new ExampleRepository(dbContext);
return new Task1(repository);
}
}
And the usage
public class MyTaskApplication
{
private readonly ITaskFactory taskFactory;
public MyApplicationService(ITaskFactory taskFactory)
{
this.taskFactory = taskFactory;
}
public void Run()
{
// Default instance with default connectionString from appsettings.json
var task1 = taskFactory.CreateNewTask();
// Tenant configuration you pass in as string
var task2 = taskFactory.CreateNewTask(tenantConnectionString);
}
}
This was my attempt at using your code in a test app, but I'm unsure if I'm doing this correctly.
I'm also unsure about how to pass in the connection string for the method in MyTaskApplication CreateNewTask(connectionString)
Will it need to be passed in as a property, or part of the constructor for MyTaskApplication or an alternative method?
public class Program
{
public static void Main(string[] args)
{
var services = new ServiceCollection();
services.AddScoped<Task1>();
services.AddScoped<MyTaskApplication>();
services.AddTransient<ITaskFactory, TaskFactory>();
var serviceProvider = services.BuildServiceProvider();
var m = serviceProvider.GetService<MyTaskApplication>();
m.Run();
}
}
public class TaskFactory : ITaskFactory
{
private readonly IServiceProvider services;
public TaskFactory(IServiceProvider services)
{
this.services = services;
}
public Task1 CreateNewTask()
{
// get default task service, which is transient as before
// so you get a new instance per call
return services.GetService<Task1>();
}
public Task1 CreateNewTask(string connectionString)
{
// i.e. when having multiple tenants and you want to
// to the task on a database which is only determined at
// runtime. connectionString is not know at compile time because
// the user may choose which one he wants to process
//var dbContext = MyDbContext(connectionString);
//var repository = new ExampleRepository(dbContext);
return new Task1(connectionString);
}
}
public interface ITaskFactory
{
Task1 CreateNewTask();
Task1 CreateNewTask(string connectionString);
}
public class MyTaskApplication
{
private readonly ITaskFactory taskFactory;
private string tenantConnectionString;
public MyTaskApplication(ITaskFactory taskFactory)
{
this.taskFactory = taskFactory;
}
public void Run()
{
// Default instance with default connectionString from appsettings.json
var task1 = taskFactory.CreateNewTask();
task1.Process();
// Tenant configuration you pass in as string
var task2 = taskFactory.CreateNewTask(tenantConnectionString);
task2.Process();
Console.WriteLine("Running");
}
}
public class Task1
{
private string _repositoryText;
public Task1()
{
_repositoryText = String.Empty;
}
public Task1(string repositoryText)
{
_repositoryText = repositoryText;
}
public void Process()
{
Console.WriteLine("process: " + _repositoryText);
}
}
I have webapi which for testing purposes I am hosting in owin. I have set it up using autofac. now when I am testing I want to inject moq dependencies. which I am not able to so far. I have read the documentation and did bit of research but I am missing something.
here is the testing code.
[Test]
public void Request_all_airports()
{
const int port = 8086;
AirportCollection moqAirportCollection = new AirportCollection();
moqAirportCollection.Airports = new List<Airport>{new Airport{IATA = "moq",Name = "moqName"}};
using (WebApp.Start<Startup>("http://localhost:" + port))
{
using (var mock = AutoMock.GetLoose())
{
var moqObj = mock.Mock<IAirportService>().Setup(x => x.GetAirports()).Returns(moqAirportCollection);
var client = new HttpClient {BaseAddress = new Uri("http://localhost:" + port)};
var response = client.GetAsync("/api/airport/get").Result;
var body = response.Content.ReadAsStringAsync().Result;
var airportCollection = JsonConvert.DeserializeObject<AirportCollection>(body);
}
}
}
Please have a look. let me know what I am missing. if you want to look at controller code or any other piece do let me know .
here is code for startup
public class Startup
{
public static IContainer container { get; set; }
public void Configuration(IAppBuilder appBuilder)
{
var httpConfig = new HttpConfiguration();
container = AutofacSetup.Register(httpConfig);
WebApiConfig.Register(httpConfig);
appBuilder.UseAutofacMiddleware(container);
appBuilder.UseAutofacWebApi(httpConfig);
appBuilder.UseWebApi(httpConfig);
}
}
Thanks
I think so I have solved it with help from people. here is my code.
var moq = new Mock<IAirportService>();
moq.Setup(x => x.GetAirports()).Returns(moqAirportCollection);
newBuilder.RegisterInstance(moq.Object).As<IAirportService>();
newBuilder.Update(Startup.container);
I havnt rebuild the contrain i just updated it. autofac have behavior to use latest registration so it will use mocked on here.
You are almost there.
In your test you need to register your mock service with your autofac container so that dependencies on IAirportService are resolved with the mock in the application.
One way to achieve this is override the Startup class' Configuration method for each test and put your test DI in there. I've put some comments below to show changes that can be made:
public class Startup
{
public static IContainer container { get; set; }
// make this virtual
public virtual void Configuration(IAppBuilder appBuilder)
{
var httpConfig = new HttpConfiguration();
// have this return the ContainerBuilder instead of the container
var builder = AutofacSetup.Register(httpConfig)
container = builder.Build();
WebApiConfig.Register(httpConfig);
appBuilder.UseAutofacMiddleware(container);
appBuilder.UseAutofacWebApi(httpConfig);
appBuilder.UseWebApi(httpConfig);
}
}
Then in your test class, derive from the Startup class and put your test logic in. Something like this:
public class MyTestCase {
public static Mock<IAirportService> MockObj { get; set; }
private class TestStartup : Startup {
public override void Configuration(IAppBuilder app) {
var httpConfig = new HttpConfiguration();
// this now returns ContainerBuilder instead of the container
var builder = AutofacSetup.Register(httpConfig)
// register your mock, change this to whatever lifetime scope you need
var moqAirportCollection = new AirportCollection();
moqAirportCollection.Airports = new List<Airport>{new Airport{IATA = "moq",Name = "moqName"}};
var mock = AutoMock.GetLoose()
MockObj = mock.Mock<IAirportService>()
.Setup(x => x.GetAirports())
.Returns(moqAirportCollection);
var moqObj = MockObj.Object;
builder.RegisterInstance(moqObj).As<IAirportService>();
container = builder.Build();
WebApiConfig.Register(httpConfig);
appBuilder.UseAutofacMiddleware(container);
appBuilder.UseAutofacWebApi(httpConfig);
appBuilder.UseWebApi(httpConfig);
}
}
[Test]
public void Request_all_airports()
{
using (var server = WebApp.Start<Startup>())
{
var response =
server.CreateRequest("/api/airport/get")
.GetAsync()
.Result;
var body = response.Content.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<AirportCollection>(body);
// assert something
}
}
}
A unit test should test a single component. In your case, you are trying to test the AirportController through a HTTP query, not the AirportController as a standalone component.
The AirportController class depends on a IAirportService component. In order to test the component without any dependency you created a moq on IAirportService. Now you can instantiate a new AirportController with this moq and run your test using this instance.
If you have a AirportController like this
public class AirportController
{
public AirportController(IAirportService airportService) { /* ... */}
}
The AirportController test should be like this :
[Test]
public void Request_all_airports()
{
AirportCollection moqAirportCollection = new AirportCollection();
var moqAirPort = new Airport{ IATA = "moq",Name = "moqName" };
moqAirportCollection.Airports = new List<Airport>{ moqAirPort };
using (var mock = AutoMock.GetLoose())
{
var moqAirportService = mock.Mock<IAirportService>()
.Setup(x => x.GetAirports())
.Returns(moqAirportCollection);
var testedAirportController = new AirportController(moqAirportService);
AirportCollection airportCollection = testedAirportController.Get();
Assert.AreEquals(1, airportCollection.Length, "Invalid number of airport");
Assert.AreEquals(moqAirPort.Name, airportCollection[0].Name, "Invalid name");
}
}
I want to start mocking some code but I am unsure how to do it. I have read a few tutorials but I cannot apply it to my code (There maybe a reason behind it).
I am using NUnit and Moq.
(I have removed all other methods so I can just show you one).
Api Interface:
public interface IApiRequest
{
Task<T> ExecuteAsync<T>(RestRequest request);
}
Method I want to Mock:
public async Task<UpcomingMovies> GetUpcomingMovies(int page)
{
var request = new RestRequest
{
Resource = "movie/upcoming",
};
request.AddParameter("page", page.ToString());
request.AddParameter("language", "en");
var api = new ApiRequest();
return await api.ExecuteAsync<UpcomingMovies>(request);
}
I'm not sure how I can mock this.
Update:
Is this now a valid test?
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(new UpcomingMovies()));
If you want to mock the ExecutAsync method you can do it like this:
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(/** whatever movies **/));
if you want to mock for a particlur request, replace It.IsAny<RestRequest>() with a reference to your request.
To effectively test your class you need something like this:
public class MyClass
{
public MyClass(IApiRequest api)
{
this.api = api;
}
public async Task<UpcomingMovies> GetUpcomingMovies(int page)
{
var request = new RestRequest
{
Resource = "movie/upcoming",
};
request.AddParameter("page", page.ToString());
request.AddParameter("language", "en");
return await api.ExecuteAsync<UpcomingMovies>(request);
}
}
Test
[Test]
public async Task MyTest()
{
var expectedMovies = new UpcomingMovies(); // or whatever movies you need
Mock<IApiRequest> mock = new Mock<IApiRequest>();
mock.Setup(x => x.ExecuteAsync<UpcomingMovies>(It.IsAny<RestRequest>()))
.Returns(Task.FromResult<UpcomingMovies>(expectedMovies));
var myClass = new MyClass(mock.Object);
var result = await myClass.GetUpcomingMovies(1);
Assert.IsTrue(expectedMovies == result);
}