0

I may doing something wrong but can't find the answer.

It seems that line

var searchFake = A.Fake<Search>(); 

should simply work. But it always gives me the error

Constructor with signature () failed: No usable default constructor was found on the type Amazon.DynamoDBv2.DocumentModel.Search.

While Search type actually has internal parameterless constructor and I simply could create instance of this type with the next code:

  var constructor = typeof(Search).GetConstructors(BindingFlags.NonPublic | 
       BindingFlags.Instance).Single(c => c.GetParameters().Length == 0);
       
  var instance = Activator.CreateInstance(typeof(Search), nonPublic: true)   
       as Search;

Why FakeItEasy unable to do so or what I'm doing wrong here?

Anton Shakalo
  • 437
  • 1
  • 6
  • 23

1 Answers1

1

Why FakeItEasy unable to do so or what I'm doing wrong here?

FakeItEasy's unable to do so because it uses Castle DynamicProxy to create the Fakes, and this is how DynamicProxy works.

Could these libraries work around the issue, possibly via a reflection trick not unlike the one you mentioned above? Probably, but it's not something they do today.

The FakeItEasy documentation, specifically the section what types can be faked?, indicates that

special steps will need to be taken to fake internal interfaces and classes

In your case, it seems the class is public, but the constructor is not, but the suggested steps apply anyhow: making the internals of the assembly containing the class to be faked visible to DynamicProxyGenAssembly2.

I don't know this for sure, but suspect that you don't have control over the assembly that contains Amazon.DynamoDBv2.DocumentModel.Search. So you might have to be a little more creative. Perhaps introduce an interface that could be implemented by a proxy class of your own (that also is derived from Amazon.DynamoDBv2.DocumentModel.Search). Then you could fake the interface. Your production code would have to change to instantiate this new class, though.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • Yeah, it's out of my control. However, I digged through sources if aws sdk last night and found that Table class might help me instantiate Search. I'll go that way or just incapsulate one more level of the abstraction to get rid of that problem at all – Anton Shakalo Jun 01 '23 at 08:50