FakeItEasy 异常方法没有实现
FakeItEasy exception method does not have an implementation
我有一个普通的单元测试,并尝试在设置方法中创建一个伪接口:
[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:
Failed to create fake of type Argus.Avenue.Data.DataService.Regeln.Actions.IAddTagAction.
Below is a list of reasons for failure per attempted constructor:
No constructor arguments failed:
No usable default constructor was found on the type Argus.Avenue.Data.DataService.Regeln.Actions.IAddTagAction.
An exception of type System.TypeLoadException was caught during this call. Its message was:
Die Methode "GetWertbezeichnung" im Typ "Castle.Proxies.ObjectProxy_1" der Assembly "DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" hat keine Implementierung.
翻译:
"The method "GetWertbezeichnung" 类型 "Castle.Proxies.ObjectProxy_1" 的程序集 "DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" 没有实现。
这里是接口&类涉及:
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);
}
AddTagAction:
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
这是一个known bug in FakeItEasy 4.1.1, due to a bug in Castle.Core。它已在 FakeItEasy 4.2.0 中修复。只需升级到较新的版本就可以了。
我有一个普通的单元测试,并尝试在设置方法中创建一个伪接口:
[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: Failed to create fake of type Argus.Avenue.Data.DataService.Regeln.Actions.IAddTagAction.
Below is a list of reasons for failure per attempted constructor: No constructor arguments failed: No usable default constructor was found on the type Argus.Avenue.Data.DataService.Regeln.Actions.IAddTagAction. An exception of type System.TypeLoadException was caught during this call. Its message was: Die Methode "GetWertbezeichnung" im Typ "Castle.Proxies.ObjectProxy_1" der Assembly "DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" hat keine Implementierung.
翻译: "The method "GetWertbezeichnung" 类型 "Castle.Proxies.ObjectProxy_1" 的程序集 "DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" 没有实现。
这里是接口&类涉及:
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);
}
AddTagAction:
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
这是一个known bug in FakeItEasy 4.1.1, due to a bug in Castle.Core。它已在 FakeItEasy 4.2.0 中修复。只需升级到较新的版本就可以了。