1

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()

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Roi
  • 41
  • 3
  • Related: [Method not found?](https://stackoverflow.com/a/8059594/2791540) – John Wu Feb 04 '20 at 00:44
  • This is your problem `dataContext.UserId = (int)UserCode.System;` Can you explain what your are trying to do with this line? Did you potentially want to just store it as a instance member? – TheGeneral Feb 04 '20 at 00:46
  • @MichaelRandall yes. so that the user for everything that is saved by the context is set to the system's usercode. – Roi Feb 04 '20 at 00:53

1 Answers1

1

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

halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • The context is the one that uses it. The Id used depends on the repository. – Roi Feb 04 '20 at 01:09
  • edited the question and added the DataContext. It was named as DataContext but Inherits DbContext. – Roi Feb 04 '20 at 01:20