0

i was reading this How to make Entity Framework Data Context Readonly because I have a read-only context that uses SQL views as entities. Is it possible to fake DbQuery so I can test my other logic? FakeItEasy returns these errors:

var _fakeRequestList = A.Fake<DbQuery<RequestList>>(opt => opt
    .Implements(typeof(IQueryable<RequestList>)));



Result Message: 
Initialization method Test.QueryTests.Initialize threw exception. FakeItEasy.Core.FakeCreationException: FakeItEasy.Core.FakeCreationException: 
  Failed to create fake of type "System.Data.Entity.Infrastructure.DbQuery`1[DAL.QueryContext.Model.RequestList]".

  Below is a list of reasons for failure per attempted constructor:
    No constructor arguments failed:
      No usable default constructor was found on the type System.Data.Entity.Infrastructure.DbQuery`1[DAL.QueryContext.Model.RequestList].
      An exception was caught during this call. Its message was:
      Parent does not have a default constructor. The default constructor must be explicitly defined.
    The following constructors were not tried:
      (*System.Data.Entity.Internal.Linq.IInternalQuery`1[DAL.QueryContext.Model.RequestList])

      Types marked with * could not be resolved, register them in the current
      IFakeObjectContainer to enable these constructors.


Result StackTrace:  
at FakeItEasy.Core.DefaultExceptionThrower.ThrowFailedToGenerateProxyWithResolvedConstructors(Type typeOfFake, String reasonForFailureOfUnspecifiedConstructor, IEnumerable`1 resolvedConstructors)
   at FakeItEasy.Creation.FakeObjectCreator.TryCreateFakeWithDummyArgumentsForConstructor(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, String failReasonForDefaultConstructor, Boolean throwOnFailure)
   at FakeItEasy.Creation.FakeObjectCreator.CreateFake(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, Boolean throwOnFailure)
   at FakeItEasy.Creation.DefaultFakeAndDummyManager.CreateFake(Type typeOfFake, FakeOptions options)
   at FakeItEasy.Creation.DefaultFakeCreatorFacade.CreateFake[T](Action`1 options)
   at FakeItEasy.A.Fake[T](Action`1 options)
   at Test.QueryTests.Initialize() in c:\SourceCode\Portal\Main\Portal\UnitTest\QueryTests.cs:line 25
Community
  • 1
  • 1
jmzagorski
  • 1,135
  • 18
  • 42
  • I wouldn't recommend it. My advice is to use either SQL CE or SQL LocalDB to fake a database. PS FakeItEasy is not a Faking framework. It is a mocking framework. You would be mocking a DbContext, not Faking one. – Aron Jun 17 '14 at 13:42
  • thanks. I actually changed `DbQuery` to `IQueryable` and set my entities in my modelbuilder and it worked. – jmzagorski Jun 17 '14 at 13:55
  • @Aron the naming really isn't so important. We tend to call them 'fakes' in FakeItEasy since we don't make any distinction between mocks, stubs etc. Many other frameworks do, with 'Mock' meaning something specific. – Adam Ralph Jun 17 '14 at 19:06
  • My usual answer to these questions is "don't unit test your data access layer". The return on investment just isn't good enough. If you thin out your DAL as much as possible and fake that instead, leaving the actual DAL not unit-tested you will probably have good results. – Adam Ralph Jun 17 '14 at 19:09
  • 1
    Thank you. I am implementing a query object pattern (using Entity Framework as my repository and UOW) so I am not really testing the DAL, I was testing the statement "if my repository brings back this do this else do that" (business logic) Since my context is readonly I could not fake the IDbSet in the traditional way. – jmzagorski Jun 18 '14 at 19:52

0 Answers0