I got an dotnet project and i was building an implementation test routine
Here is my code:
Base->BaseProject->Controllers->SensorController
[Route("[controller]")]
[ApiController]
public class SensorController : Controller
{
private readonly SensorContext _context;
public SensorController(SensorContext context)
{
_context = context;
}
[HttpGet("api/")]
public async Task<ActionResult<IEnumerable<Sensor>>> GetTodoItems()
{
return await _context.Sensor.ToListAsync();
}
}
And in my TestProject, I got this:
Base->TestProject->Fixtures->TestContext
public class TestContext
{
public HttpClient Client { get; private set; }
private TestServer _server;
public TestContext()
{
SetupClient();
}
private void SetupClient()
{
_server = new TestServer(new WebHostBuilder().UseStartup<Startup>());
Client = _server.CreateClient();
}
}
Base->TestProject->SensorApiTest.cs
public class SensorApiTest
{
private readonly TestContext _testContext;
public SensorApiTest()
{
_testContext = new TestContext();
}
[Fact]
public async Task Values_Get_ReturnsOkResponse()
{
var response = await _testContext.Client.GetAsync("/Sensor/api");
response.EnsureSuccessStatusCode();
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}
}
If i run the base project and send requests using insominia, i got the responses just fine. But running the TestProject with dotnet test return an 404 error.
The dependencies off the project are working fine, i dont know why this do not run.
Can anyone help me? Thanks
Related
I am new to Integration test, I have a controller which has IMediator and I am using Moq framework for the integration test.
The issue I am having is that I keep getting null in the response when trying to mock MediatR. Before trying to mock MediatR, I tried to mock a service (in this case IUserService) and it worked perfectly (the return type for the Delete controller and other methods was bool).
Since I am now using IMediator in the controller so trying to change the integration test to mock MediatR, I can do integration test for the handler which has IUserService but I am trying to test the whole pipeline. Below is what I have in terms of code starting from the controller to the integration test.
//Controller
private IMediator _mediator;
public UserController(IMediator mediator)
{
_mediator = mediator;
}
[HttpDelete("{id}")]
public async Task<ActionResult<Result<Unit>>> DeleteUser(int id)
{
return await _mediator.Send(new DeleteUserCommand { UserId = id });
}
public class DeleteUserCommand : IRequest<Result<Unit>> {public int UserId { get; set; }}
//Command handler
public class DeleteUserCommandHandler : IRequestHandler<DeleteUserCommand, Result<Unit>>
{
private readonly IUserService _userService;
public DeleteUserCommandHandler(IUserService userService)
{
_userService = userService;
}
public async Task<Result<Unit>> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
{
return await _userService.DeleteUser(request.UserId);
}
}
//Service layer
public async Task<Result<Unit>> DeleteUser(int userId)
{
var userExist = await _context.Users.FirstOrDefaultAsync(x => x.Id == userId);
if (userExist == null) return Result<Unit>.Failure("User Id doesn't exsist");
_context.Remove(userExist);
var result = await _context.SaveChangesAsync() > 0;
return Result<Unit>.Success(Unit.Value);
}
//Integration test
[TestFixture]
public class UserControllerTests
{
private readonly Mock<IMediator> _mockMediator;
private UserController _userController;
public UserControllerTests()
{
_mockMediator = new Mock<IMediator>();
}
[Test]
public async Task DeleteUser_NotSuccessful_NoIdDeleted()
{
Result<Unit> expected = new Result<Unit>();
_mockMediator.Setup(x => x.Send(It.IsAny<DeleteUserCommand>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(expected));
_userController = new UserController(_mockMediator.Object);
var result = await _userController.DeleteUser(6);
Assert.AreEqual("User Id doesn't exsist", result?.Value?.Error);
}
}
//Response in the integration test
I have two UserIds 6 and 7
UserId 6: doesn't exsist so I am expecting a message saying id doesn't exsist but the current response I am getting is null.
UserId 7: does exsist and expecting something like IsSuccess: true which is a custom code I added
Note: the attached code for the test is just for user Id 6.
You might notice in the code above starting from the controller, part of the return type is Result which is a custom class I added and below is the code.
public class Result<T>
{
public bool IsSuccess { get; set; }
public T Value { get; set; }
public string Error { get; set; }
public static Result<T> Success(T value) => new Result<T> { IsSuccess = true, Value = value };
public static Result<T> Failure(string error) => new Result<T> { IsSuccess = false, Error = error };
}
`
I am trying to find out what I have done wrong with mocking MediatR and why it keep returning null.
Thanks for the help in advance.
First thing I tried to do integration test mock the IUserService then switched to IMediator then started to get null in the response when doing integration test. I tried to google the issue but no luck.
I have consumer CarCreatedConsumer which I want to unit test
public class CarCreatedConsumer: IConsumer<CarCreatedEvent>
{
private readonly IMediator _mediator;
public CarCreatedConsumer(IMediator mediator)
{
_mediator = mediator;
}
public async Task Consume(ConsumeContext<CarCreatedEvent> context)
{
....
}
}
Using MassTransit.Testing I'm trying to write test for event consumer
[TestFixture]
public class MyTests
{
private ServiceProvider _provider;
private InMemoryTestHarness _harness;
[SetUp]
public void SetUp()
{
_provider = new ServiceCollection()
.AddMassTransitInMemoryTestHarness(cfg =>
{
cfg.AddConsumer<CarCreatedConsumer>();
}).AddMediator()
.BuildServiceProvider(true);
_harness = _provider.GetRequiredService<InMemoryTestHarness>();
}
[Test]
public async Task MessageShouldBeConsumed()
{
await _harness.Start();
try
{
await _harness.InputQueueSendEndpoint.Send<CarCreatedEvent>(new
{
CarId = Guid.NewGuid.ToString(),
CarOwnerName = "John Stuart"
...
}
// Always false
Assert.True(_harness.Consumed.Select<CarCreatedEvent>().Any());
}
finally
{
await _harness.Stop();
}
}
}
I'm expect this to return true but it fails (returns false). Obviously I'm doing something wrong, any ideas?
Your test harness usage is suspect, and is also using an obsolete version of the configuration. I'd suggest reading the documentation on the current test harness and how to test your consumer.
If I have a controller class, and I want to pass it to a different namespace that handles my HTTP calls, such as in the below scenario, Main task calls TaskA() which calls TaskG(), do I need to pass it to TaskG via A like the below? Or is there someway to just create it in the namespace HttpClassFurtherDown without the calling classes needing to pass it.
namespace Controllers{
public class Drawings : ControllerBase
{
private IHttpClientFactory _client {get;set;}
public Drawings(IHttpClientFactory client)
{
_client=client;
}
[Route("api/Drawings")]
[HttpPost]
public async Task<ActionResult> PostAsync([FromBody] JsonFileContent[] content)
{
HttpExample e = new HttpExample(_client);
e.TaskA();
TaskB();
return Accepted($"Drawings/Job/{id}");
}
}}
namespace HttpClassExample{
public class HttpExample
{
private IHttpClientFactory _client {get;set;}
public HttpExample(IHttpClientFactory client)
{
_client=client;
}
public void TaskA()
{
DoSomeProcessing();
HttpClassExampleFurtherDown e = new HttpClassExampleFurtherDown(client);
e.TaskG();
}
}
}
namespace HttpClassExampleFurtherDown{
public class HttpExampleFurtherDown
{
private IHttpClientFactory _client {get;set;}
public HttpExampleFurtherDown(IHttpClientFactory client)
{
_client=client;
}
public void TaskG(client)
{
//Finally Using It Here. I want to avoid having to generate it at the controller level and pass it all the way down.
client.CreateClient();
client.SendAsync();
}
}
}
I want to avoid having to generate it at the controller level and pass it all the way down.
If following DIP then inject explicit dependencies where they are actually needed instead of tightly coupling to implementation concerns.
While I believe the example provided are oversimplified, here is what the example above should look like
Controllers.Drawings
namespace Controllers{
using HttpClassExample;
//...
public class Drawings : ControllerBase {
private readonly IHttpExample client;
public Drawings(IHttpExample client) {
this.client = client;
}
[Route("api/Drawings")]
[HttpPost]
public async Task<ActionResult> PostAsync([FromBody] JsonFileContent[] content) {
await client.TaskA();
TaskB();
return Accepted($"Drawings/Job/{id}");
}
}
}
HttpClassExample.HttpExample
namespace HttpClassExample{
using HttpClassExampleFurtherDown;
//...
public class HttpExample : IHttpExample{
private readonly IHttpExampleFurtherDown client;
public HttpExample(IHttpExampleFurtherDown client) {
this.client = client;
}
public async Task TaskA() {
DoSomeProcessing();
await client.TaskG();
}
//...
}
}
HttpClassExampleFurtherDown.HttpExampleFurtherDown
namespace HttpClassExampleFurtherDown{
public class HttpExampleFurtherDown : IHttpExampleFurtherDown {
private readonly IHttpClientFactory factory;
public HttpExampleFurtherDown(IHttpClientFactory factory) {
this.factory = factory;
}
public async Task TaskG() {
HttpClient client = factory.CreateClient();
//...
var response = await client.SendAsync();
//...
}
}
}
This assumes that a container is being used to manage the resolution and injection of dependency implementations based on their registered abstractions
i use Mediator in my project .
Demo Project on Github
i want to use TDD in my project and integration test with .Net core 3.0
i write this code int test class for use intergration test with mediator :
public class SubscribeTest : IClassFixture<TravelRequest<Startup>>, IClassFixture<DbContextFactory>, IDisposable
{
private readonly TravelRequest<Startup> request;
private readonly DbContextFactory contextFactory;
public SubscribeTest(TravelRequest<Startup> request , DbContextFactory contextFactory)
{
this.request = request;
this.contextFactory = contextFactory;
}
public void Dispose()
{
request.Dispose();
}
[Fact]
public async Task ListSubscribeAsync()
{
var add = await request.Get("/Subscribe/GetListSubscribe");
await add.BodyAs<SubscribListDto>();
}
}
and this is TravelRequest :
public class TravelRequest<TStartup> : IDisposable where TStartup : class
{
private readonly HttpClient client;
private readonly TestServer server;
public TravelRequest()
{
var webHostBuilder = new WebHostBuilder().UseStartup<TStartup>().UseConfiguration(ConfigorationSingltonConfigoration.GetConfiguration());
this.server = new TestServer(webHostBuilder);
this.client = server.CreateClient();
}
}
and this is ConfigorationSingltonConfigoration for use the appSetting-test.json :
public class ConfigorationSingltonConfigoration
{
private static IConfigurationRoot configuration;
private ConfigorationSingltonConfigoration() { }
public static IConfigurationRoot GetConfiguration()
{
if (configuration is null)
configuration = new ConfigurationBuilder()
.SetBasePath(Path.Combine(Path.GetFullPath("../../../")))
.AddJsonFile("appsettings-test.json")
.AddEnvironmentVariables()
.Build();
return configuration;
}
}
and finally this is for set DbContext :
public class DbContextFactory : IDisposable
{
public TravelContext Context { get; private set; }
public DbContextFactory()
{
var dbBuilder = GetContextBuilderOptions<TravelContext>("SqlServer");
Context = new TravelContext(dbBuilder.Options);
Context.Database.Migrate();
}
public void Dispose()
{
Context.Dispose();
}
public TravelContext GetRefreshContext()
{
var dbBuilder = GetContextBuilderOptions<TravelContext>("SqlServer");
Context = new TravelContext(dbBuilder.Options);
return Context;
}
private DbContextOptionsBuilder<TravelContext> GetContextBuilderOptions<T>(string connectionStringName)
{
var connectionString = ConfigorationSingltonConfigoration.GetConfiguration().GetConnectionString(connectionStringName);
var contextBuilder = new DbContextOptionsBuilder<TravelContext>();
var servicesCollection = new ServiceCollection().AddEntityFrameworkSqlServer().BuildServiceProvider();
contextBuilder.UseSqlServer(connectionString).UseInternalServiceProvider(servicesCollection);
return contextBuilder;
}
}
Now my problem is here , when i RunTest in result it show me this error :
---- System.InvalidOperationException : Unable to resolve service for type 'System.Collections.Generic.IList1[FluentValidation.IValidator1[Travel.Services.SubscribeServices.Query.SubscribeList.SubscribeListCommand]]' while attempting to activate 'Travel.Common.ValidateBehavior.ValidateCommandBehavior2[Travel.Services.SubscribeServices.Query.SubscribeList.SubscribeListCommand,Travel.Common.Operation.OperationResult1[System.Collections.Generic.IEnumerable`1[Travel.ViewModel.SubscibeDto.SubscribListDto]]]'. ---- System.NotImplementedException : The method or operation is not implemented.
whats the problem ? how can i solve ths problem ??? i put the source code of project in top of question
I suspect that the validators are not registered into the container.
You can use the FluentValidation in .NET Core with Dependency Injection, by installing the nuget and than passing the assembly.
More references at Using FluentValidation in .NET Core with Dependency Injection.
More reference of how you can do this at Fluent Validation on .NET Core 2.0 does not register.
I'm using Owin.Testing as test env. In my controller i need to get remote ip address from the caller.
//in my controller method
var ip = GetIp(Request);
Util
private string GetIp(HttpRequestMessage request)
{
return request.Properties.ContainsKey("MS_HttpContext")
? (request.Properties["MS_HttpContext"] as HttpContextWrapper)?.Request?.UserHostAddress
: request.GetOwinContext()?.Request?.RemoteIpAddress;
}
As a result Properties does not contains MS_HttpContext and RemoteIpAddress of OwinContext is null.
Is there any option to get IP?
Found the solution. Use testing middleware for this. Everything in your tests project:
public class IpMiddleware : OwinMiddleware
{
private readonly IpOptions _options;
public IpMiddleware(OwinMiddleware next, IpOptions options) : base(next)
{
this._options = options;
this.Next = next;
}
public override async Task Invoke(IOwinContext context)
{
context.Request.RemoteIpAddress = _options.RemoteIp;
await this.Next.Invoke(context);
}
}
Handler:
public sealed class IpOptions
{
public string RemoteIp { get; set; }
}
public static class IpMiddlewareHandler
{
public static IAppBuilder UseIpMiddleware(this IAppBuilder app, IpOptions options)
{
app.Use<IpMiddleware>(options);
return app;
}
}
Testing startup:
public class TestStartup : Startup
{
public new void Configuration(IAppBuilder app)
{
app.UseIpMiddleware(new IpOptions {RemoteIp = "127.0.0.1"});
base.Configuration(app);
}
}
And then create test server via TestStartup:
TestServer = TestServer.Create<TestStartup>();