xeraphim
xeraphim

Reputation: 4645

FakeItEasy exception method does not have an implementation

I have an ordinary unit test and try to create a fake of an interface in the setup-method:

[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);
    }

Unfortunately, on the line var addTagAction = A.Fake<IAddTagAction>(); I get the following exception:

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.

Translation: "The method "GetWertbezeichnung" in type "Castle.Proxies.ObjectProxy_1" of assembly "DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" does not have an implementation.

Here are the interface & classes involved:

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;
        }
    }

BaseAction:

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);
}

Thanks in advance

Edit: If I remove the "GetWertbezeichnung"-methods, the fake-creation works.. it must have something to do with these methods...

Edit2: The versions we're using are:

Upvotes: 0

Views: 637

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292425

It's a known bug in FakeItEasy 4.1.1, due to a bug in Castle.Core. It's fixed in FakeItEasy 4.2.0. Just upgrade to a newer version and you should be fine.

Upvotes: 3

Related Questions