c# - FakeItEasy 异常方法没有实现
问题描述
我有一个普通的单元测试,并尝试在设置方法中创建一个假的接口:
[TestInitialize]
public void Setup()
{
var unityContainer = A.Fake<IUnityContainer>();
var addTagAction = A.Fake<IAddTagAction>();
A.CallTo(() => unityContainer.Resolve(typeof(IAddTagAction), null, A<ResolverOverride[]>._)).Returns(addTagAction);
this.testee = new ActionFactory(unityContainer);
}
不幸的是,在线var addTagAction = A.Fake<IAddTagAction>();
我得到以下异常:
Die Initialisierungsmethode 'Argus.Avenue.DataService.Test.Regeln.ActionFactoryTest.Setup' hat eine Ausnahme ausgelöst。FakeItEasy.Core.FakeCreationException:无法创建 Argus.Avenue.Data.DataService.Regeln.Actions.IAddTagAction 类型的假。
以下是每个尝试构造函数失败的原因列表: 没有构造函数参数失败:在类型 Argus.Avenue.Data.DataService.Regeln.Actions.IAddTagAction 上找不到可用的默认构造函数。在此调用期间捕获了 System.TypeLoadException 类型的异常。它的信息是:Die Methode "GetWertbezeichnung" im Typ "Castle.Proxies.ObjectProxy_1" der Assembly "DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" hat keine Implementierung。
翻译: “程序集“DynamicProxyGenAssembly2,Version=0.0.0.0,Culture=neutral,PublicKeyToken=null”的类型“Castle.Proxies.ObjectProxy_1”中的方法“GetWertbezeichnung”没有实现。
以下是涉及的接口和类:
IAddTagAction:
public interface IAddTagAction : IBaseAction
{
}
IBaseAction:
public interface IBaseAction
{
void Execute(IList<long> artikelIds, int? id, RegelModel regelModel);
string GetWertbezeichnung(int? wert);
string GetWertbezeichnung(IList<int> werte);
}
添加标签操作:
public class AddTagAction : BaseAction, IAddTagAction
{
public AddTagAction(
IEfContextFactory efContextFactory,
IRepositoryFactory repositoryFactory,
IDateTimeProvider dateTimeProvider)
: base(efContextFactory, repositoryFactory, dateTimeProvider)
{
}
public override void Execute(IList<long> artikelIds, int? tagId, RegelModel regelModel)
{
// ...
}
/// <inheritdoc />
public override string GetWertbezeichnung(IList<int> werte)
{
using (var context = this.EfContextFactory.Create(RequestInfo))
{
var tagRepository = this.RepositoryFactory.Create<ITagRepository>(context, RequestInfo);
var tags = tagRepository.GetTagNames(werte.ToList()).FirstOrDefault();
return tags.Value;
}
}
基本动作:
public abstract class BaseAction : IBaseAction
{
protected BaseAction(IEfContextFactory efContextFactory, IRepositoryFactory repositoryFactory, IDateTimeProvider dateTimeProvider)
{
this.EfContextFactory = efContextFactory;
this.RepositoryFactory = repositoryFactory;
this.DateTimeProvider = dateTimeProvider;
}
protected IRepositoryFactory RepositoryFactory { get; }
protected IEfContextFactory EfContextFactory { get; }
protected IDateTimeProvider DateTimeProvider { get; }
public virtual void Execute(IList<long> artikelIds, int? id, RegelModel regelModel)
{
// ...
}
public string GetWertbezeichnung(int? wert)
{
if (!wert.HasValue) {
return string.Empty;
}
var werte = new List<int> { wert.Value };
return GetWertbezeichnung(werte);
}
public abstract string GetWertbezeichnung(IList<int> werte);
}
- 为什么在创建 IAddTagAction 接口的假冒时出现此异常?
提前致谢
编辑:如果我删除“GetWertbezeichnung”-方法,虚假创作作品..它必须与这些方法有关......
Edit2:我们使用的版本是:
- 目标框架:
.NET Framework 4.6.2
- 平台目标:
x64
- FakeItEasy:
4.1.1
MSTest.TestAdapter 1.2.0
MSTest.TestFramework 1.2.0
解决方案
由于 Castle.Core 中的错误,这是 FakeItEasy 4.1.1 中的一个已知错误。它已在 FakeItEasy 4.2.0 中修复。只需升级到较新的版本,您应该没问题。
推荐阅读
- django - django 项目总是尝试(并且失败)在单独的应用程序中加载文件
- android - 隐式深层链接无法正常工作 - onCreate 调用了两次
- javascript - 如何将对象附加到 JSON 文件(涉及:JavaScript、Discord.js 和 JSON)
- node.js - 如何将对象保存到自定义集合?
- opencv - 系统错误:
返回带有错误集的结果 - git - 重新排序本地提交
- python - 将整数旋转到固定位数
- html - ALT 文本未显示在 Outlook、Yahoo 等主要电子邮件客户端上
- java - 如何在 Java 中打开 LMDB 数据库?
- javascript - Node js pdf-make lib 剂量是否支持大数据?