Reputation: 41
I have a unit test which calls a repository with a setter for the user Id of the DataContext. When running the tests, I am encountering a System.MissingMethodException.
CrewRepository:
public CrewRepository(DataContext dataContext) : base(dataContext)
{
dataContext.UserId = (int)UserCode.System;
}
APIFixture:
services.AddDbContext<CrewAdapter.DataContext>(options => options
.UseInMemoryDatabase("CrewUnitTestingDatabase")
.ConfigureWarnings(x => x.Ignore(InMemoryEventId.TransactionIgnoredWarning)));
services.AddScoped<CrewRepository>(x =>
{
// This is line 42:
return new CrewRepository(x.GetRequiredService<CrewAdapter.DataContext>());
});
CrewRepositoryTests:
public void RecordExists_SequenceNumberIsGreaterThanExistingRequest_MarkAsUpdate()
{
using (var scope = _serviceProvider.CreateScope())
{
APIDataBuilder.PopulateContext(scope);
var bookingRequest = new BookingRequest
{
CrewStaffNumber = "TEST",
FirstName = "Test Paul",
MiddleName = "",
LastName = "Test George",
Rank = "TEST",
Appointment = "TEST"
};
// This is line 218:
var result = scope.ServiceProvider.GetRequiredService<CrewRepository>()
.CreateBookingRequest(bookingRequest);
Assert.Equal((int)BookingRequestStatus.Update, result.Status);
}
}
DataContext:
public class DataContext : DbContext
{
public int UserId { get; set; }
public string ConnectionString { get; set; }
// *bunch of entities*
}
Error Message:
[2/4/2020 8:16:50 AM Error] [xUnit.net 00:00:01.83] Crew.Tests.Api.Repositories.CrewRequestRepositoryTests.RecordExists_SequenceNumberIsGreaterThanExistingRequest_MarkAsUpdate [FAIL] [2/4/2020 8:16:50 AM Informational] [xUnit.net 00:00:01.83] System.MissingMethodException : Method not found: 'Void Crew.DataAdapter.DataContext.set_UserId(Int32)'. [2/4/2020 8:16:50 AM Informational] [xUnit.net 00:00:01.83] Stack Trace: [2/4/2020 8:16:50 AM Informational] [xUnit.net 00:00:01.83] at Repositories.CrewRepository..ctor(DataContext dataContext) [2/4/2020 8:16:50 AM Informational] [xUnit.net 00:00:01.83] C:\Users\source\repos\Crew.Tests\Api\APIFixture.cs(42,0): at Crew.Tests.Api.APIFixture.<>c.<.ctor>b__4_2(IServiceProvider x) [2/4/2020 8:16:50 AM Informational] [xUnit.net 00:00:01.83] at lambda_method(Closure , ServiceProviderEngineScope ) [2/4/2020 8:16:50 AM Informational] [xUnit.net 00:00:01.83] at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) [2/4/2020 8:16:50 AM Informational] [xUnit.net 00:00:01.83] at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) [2/4/2020 8:16:50 AM Informational] [xUnit.net 00:00:01.83] C:\Users\source\repos\Crew.Tests\Api\Repositories\CrewRequestRepositoryTests.cs(218,0): at Crew.Tests.Api.Repositories.CrewRequestRepositoryTests.RecordExists_SequenceNumberIsGreaterThanExistingRequest_MarkAsUpdate()
Upvotes: 1
Views: 472
Reputation: 81493
DataContext
is a System.Data.Linq
Class. It can't have that field.
You will need to pass it in.
public CrewRepository(MyFunkyContext dataContext) : base(dataContext)
{
dataContext.UserId = (int)UserCode.System;
}
If your class is actually named DataContext
then you will likely need to qualify it with a full namespace or a using alias, so there is no confusion.
public CrewRepository(MyNamesSpace.Somethingelse.DataContext dataContext) : base(dataContext)
{
dataContext.UserId = (int)UserCode.System;
}
or
using DataContext = MyNamesSpace.Somethingelse.DataContext;
public CrewRepository(DataContext dataContext) : base(dataContext)
The problem seems to be it's picking the wrong DataContext.
I would suggest naming your DbContext
as something more specific e.g MyContext
Upvotes: 1