0

I am writing an unit test to test my EF project . while doing the test I am getting an error while configuraing the fake for the DBSet. My Call stack is

System.NotImplementedException
The member 'IQueryable.ElementType' has not been implemented on type 
'DbSet`1Proxy_10' which inherits from 'DbSet`1'. Test doubles for 'DbSet`1' 
must provide implementations of methods and properties that are used.
at 
system.Data.Entity.Infrastructure.DbQuery`1.GetInternalQueryWithCheck(String 
memberName)
 at 

My Unit Test Code is - I had added a mapping data

var socMappingList = new List<DFC_SocMappings>()
        {
            new DFC_SocMappings()
            {
                JobProfile = "Police Officer",
                ONetCode = "111-00.01",
                QualityRating = 4,
                SocCode = "1120"
            },
            new DFC_SocMappings()
            {
                JobProfile = "Police Officer",
                ONetCode = "111-00.02",
                QualityRating = 4,
                SocCode = "1121"
            },
        };
       //Arrange
        var applicationLogger = A.Fake<IApplicationLogger>();
        var fakeIQueryable = new List<DFC_SocMappings>().AsQueryable();
        var context = A.Fake<IObjectContextFactory<OnetRepositoryDbContext>>();

        // var fakeDbSet = A.Fake<DbSet<DFC_SocMappings>>();
        IMapper iMapper = new AutoMapper.Mapper(new MapperConfiguration(cfg => cfg.AddProfile(new SkillsFrameworkMapper())));
        var fakeDbSet = A.Fake<DbSet<DFC_SocMappings>>((d => d.Implements(typeof(IQueryable<DFC_SocMappings>)).Implements(typeof(IDbAsyncEnumerable<DFC_SocMappings>)))).SetupData(socMappingList);

        A.CallTo(() => context.GetContext().DFC_SocMappings).Returns(fakeDbSet);

        var ct = A.Fake<OnetRepository>(op => op.WithArgumentsForConstructor(new object[] { context, iMapper, applicationLogger }));
        //Act
         A.CallTo(() => ((IQueryable<DFC_SocMappings>)fakeDbSet).Provider).Returns(fakeIQueryable.Provider);
        A.CallTo(() => ((IQueryable<DFC_SocMappings>)fakeDbSet).Expression).Returns(fakeIQueryable.Expression);
        A.CallTo(() => ((IQueryable<DFC_SocMappings>)fakeDbSet).ElementType).Returns(fakeIQueryable.ElementType);
         A.CallTo(() => ((IQueryable<DFC_SocMappings>)fakeDbSet).GetEnumerator()).Returns(fakeIQueryable.GetEnumerator());




        //Assert
        var onetRespository = new OnetRepository(context, iMapper, applicationLogger);
        var response = await onetRespository.GetAllSocMappingsAsync<DfcOnetSocMappings>();
        var res = response; // failing while reading the data

        A.CallTo(() => context.GetContext().Set<DFC_SocMappings>()).MustHaveHappened();

        response.Should().NotBeNull();
        response.Should().BeSameAs(socMappings);
        onetRespository.Dispose();

Can someone tell what mistake I am doing.

Dinesh
  • 193
  • 2
  • 14
  • No sure if that's your issue. But in case of my implementation using Moq I'm also mocking the GetEnumerator method of the db set with the Enumerator of the queryable collection (as IAsyncEnumerable) – Simon Katanski Jul 20 '18 at 12:45
  • It's hard to tell what's going wrong with the truncated stack trace and so much missing code. Since you clearly implmented `IQueryable.ElementType` on your `fakeDbSet`, I suspect that it's not being used somewhere. Debug and check the fake DbSet for reference equality with the one that's throwing the exception. I bet you'll see they're different. I suspect there's an unconfigured method on some fake (likely `context`) that's returning a different fake `DbSet`. Try configuring it to return your `fakeDbSet`. – Blair Conrad Jul 20 '18 at 15:59
  • 1
    Thanks Blair. Found the issue. My linq was not using the DbSet which i had used as an interface for the unit test. the linq should use DBset to return the Element Type. – Dinesh Jul 21 '18 at 10:47

0 Answers0