Note that there are some explanatory texts on larger screens.

plurals
  1. POFaking/mocking an interface gives "no default constructor" error, how can that be?
    text
    copied!<p>I'm trying to write a unit test of a repository implementation. The repository uses RavenDB as a database. For the unit tests, I would like to mock the RavenDB parts. In order to create the mocks (fakes) I'm using FakeItEasy. I figured there wouldn't be any problems with the mocking/faking since the RavenDB API is accessed through interfaces.</p> <p>I do however have a problem when trying to instantiate a specific mock. The relevant parts of my unit test code looks like this:</p> <pre><code>[Fact] public void Test() { UserDocument doc = ...; IQueryable&lt;UserDocument&gt; where = A.Fake&lt;IQueryable&lt;UserDocument&gt;&gt;(); A.CallTo(() =&gt; where.First()).Returns(doc); IRavenQueryable&lt;UserDocument&gt; query = A.Fake&lt;IRavenQueryable&lt;UserDocument&gt;&gt;(); IDocumentSession session = A.Fake&lt;IDocumentSession&gt;(); A.CallTo(() =&gt; session.Query&lt;UserDocument&gt;()).Returns(query); IDocumentStore store = A.Fake&lt;IDocumentStore&gt;(); A.CallTo(() =&gt; store.OpenSession()).Returns(session); . . . } </code></pre> <p>When instantiating the IRavenQueryable fake I get an exception. This is the log from the Xunit.net runner:</p> <pre><code>UnitTest.Test : FakeItEasy.Core.FakeCreationException : Failed to create fake of type "System.Linq.IQueryable`1[UserDocument]". Below is a list of reasons for failure per attempted constructor: No constructor arguments failed: No default constructor was found on the type System.Linq.IQueryable`1[UserDocument]. Stack Trace: vid FakeItEasy.Core.DefaultExceptionThrower.ThrowFailedToGenerateProxyWithResolvedConstructors(Type typeOfFake, String reasonForFailureOfUnspecifiedConstructor, IEnumerable`1 resolvedConstructors) vid FakeItEasy.Creation.FakeObjectCreator.TryCreateFakeWithDummyArgumentsForConstructor(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, String failReasonForDefaultConstructor, Boolean throwOnFailure) vid FakeItEasy.Creation.FakeObjectCreator.CreateFake(Type typeOfFake, FakeOptions fakeOptions, IDummyValueCreationSession session, Boolean throwOnFailure) vid FakeItEasy.Creation.DefaultFakeAndDummyManager.CreateFake(Type typeOfFake, FakeOptions options) vid FakeItEasy.Creation.DefaultFakeCreatorFacade.CreateFake[T](Action`1 options) </code></pre> <p>The "no default constructor found" doesn't make any sense since what I'm trying to fake is an interface. Does anyone have a suggestion what the problem might be?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload