简体   繁体   中英

System.MissingMethodException - Setter in Constructor

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\\CrewReq uestRepositoryTests.cs(218,0): at Crew.Tests.Api.Repositories.CrewRequestRepositoryTests.RecordExists_SequenceNumberIsGreaterThanExistingRequest_MarkAsUpdate()

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 eg MyContext

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM