使用FakeItEasy创建Amazon.DynamoDBv2.DocumentModel.Search的伪造对象

huangapple go评论57阅读模式
英文:

Creation fake of Amazon.DynamoDBv2.DocumentModel.Search with FakeItEasy

问题

我可能做错了什么,但找不到答案。

似乎这行代码

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

应该可以正常工作。但它总是给我一个错误

构造函数签名()失败:
Amazon.DynamoDBv2.DocumentModel.Search类型上找不到可用的默认构造函数。

而Search类型实际上有一个内部的无参数构造函数,我只需使用下面的代码创建此类型的实例:

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;

为什么FakeItEasy无法做到这一点,或者我在这里做错了什么?

英文:

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

It seems that line

var searchFake = A.Fake&lt;Search&gt;(); 

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 =&gt; 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?

答案1

得分: 1

FakeItEasy无法做到这一点,或者我在这里做错了什么?

FakeItEasy无法做到这一点,因为它使用Castle DynamicProxy来创建伪装对象,这就是DynamicProxy的工作原理。

这些库能否通过反射技巧来解决这个问题,可能类似于你上面提到的方法?可能可以,但目前它们并没有这样做。

FakeItEasy的文档,特别是可以伪装哪些类型?部分指出

需要采取特殊步骤来伪装内部接口和类

在你的情况下,看起来这个类是公共的,但构造函数不是,但建议的步骤仍然适用:使包含要伪装的类的程序集的内部对DynamicProxyGenAssembly2可见。

我不确定这一点,但我怀疑你无法控制包含Amazon.DynamoDBv2.DocumentModel.Search的程序集。所以你可能需要更有创意。也许引入一个接口,可以由你自己的代理类实现(该类也继承自Amazon.DynamoDBv2.DocumentModel.Search)。然后你可以伪装这个接口。不过,你的生产代码将不得不更改以实例化这个新类。

英文:

> 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.

huangapple
  • 本文由 发表于 2023年6月1日 02:44:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376458.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定