Autofixture: Unable to create a Fake

Created on 4 Jun 2018  ·  4Comments  ·  Source: AutoFixture/AutoFixture

I'm in the process of updating a project I was working on a few years ago. In the process I have updated to the latest of FakeItEasy and AutoFixture.

I have 40 tests that are failing with an error similar to:

AutoFixture was unable to create an instance from
FakeItEasy.Fake1[System.Collections.Generic.IList1[AllOverIt.Evaluator.AoiFormulaTokenProcessorContext]] because creation unexpectedly failed with exception.

Where the inner exception reports:
Failed to create fake of type System.Collections.Generic.IList1[AllOverIt.Evaluator.AoiFormulaTokenProcessorContext]`

The line that's failing is the equivalent of this:
Fixture.Create<Fake<IList<AoiFormulaTokenProcessorContext>>>();

As a test I tried something simpler:
Fixture.Create<Fake<IList<int>>>();

This did not throw so I went looking at the constructor of AoiFormulaTokenProcessorContext. It looks like this:

public AoiFormulaTokenProcessorContext(Func<char, bool, bool> predicate, Func<char, bool, bool> processor)

After some reading at https://github.com/AutoFixture/AutoFixture/issues/984 I tried adding 'GenerateDelegates' to this:
Fixture.Customize(new AutoFakeItEasyCustomization {GenerateDelegates = true});

But I've still got the same problem.

As this code was working I can only assume there's another breaking change somewhere that I haven't yet stumbled across. Hoping someone can throw a suggestion or two my way.

TIA.

question

Most helpful comment

I understand. With the decades of experience I have I should know better - I've been in the same boat and ask the very same. Thanks for the responses.

All 4 comments

Thanks for sharing the scenario!

As usual, may I ask you to provide Minimal, Complete, and Verifiable example, so I can copy-paste it to deeply investigate the reason? The devil is often in some specifics, so even if you described the scenario good, it could still happen that some non-mentioned detail matters.

Thanks a lot in advance! :relieved:

Sorry, I should have gone and created another (possibly pair of) projects - I was hoping my question may have prompted something obvious before I went off and did that as it would have involved converting my wrapper methods into raw AutoFixture / FakeItEasy code.

The good news is I seem to have fixed it.

The AoiFormulaTokenProcessorContext class causing the problem is internal (in another project). When I changed it to public the error goes away.

I already had [assembly: InternalsVisibleTo("...")] in the AssemblyInfo.cs file but I just remembered my old projects also included [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")].

I don't recall what the latter does but the errors are now all gone.

Good news the issue is mitigated :relaxed: That's the reason I asked for the sample - it's always easier when other factors are isolated.

I don't recall what the latter does but the errors are now all gone.

I know :smile: It's related to the Fakes and Castle's proxies, used under the hood. Yep, if class is internal, then it indeed should help.

I understand. With the decades of experience I have I should know better - I've been in the same boat and ask the very same. Thanks for the responses.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ploeh picture ploeh  ·  3Comments

ploeh picture ploeh  ·  7Comments

zvirja picture zvirja  ·  3Comments

ecampidoglio picture ecampidoglio  ·  7Comments

Ephasme picture Ephasme  ·  3Comments