How can I Freeze a null instance in AutoFixture

ⅰ亾dé卋堺 提交于 2019-12-10 12:53:52

问题


I'm using Autofixture as a SUT factory and am having difficulty Freezing null instances.

I'd like to do something like:

_fixture.Freeze<IPayPalConfiguration>(c => null);

but quickly realized that was wrong. I've worked around the problem using this:

_fixture.Inject((IMyInterface)null);

but it doesn't seem right.

Hopefully someone will contribute the correct solution to the HiveMind.


回答1:


Internally, Freeze creates an instance of the requested type (e.g. IPayPalConfiguration) and then injects it so it will always return that instance when you request it again.

In that particular case, by doing _fixture.Inject((IPayPalConfiguration)null) you inject the null reference manually so you skip the creation part of the Freeze method. You freeze the IPayPalConfiguration to a single value (actually, a null reference here) but in a manual way.

Keep in mind that this _fixture.Freeze<IPayPalConfiguration>(c => null) passes a null reference for the method that will try to resolve the IPayPalConfiguration and for that reason an ArgumentNullException is thrown.



来源:https://stackoverflow.com/questions/12218206/how-can-i-freeze-a-null-instance-in-autofixture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!