How to fake delegates with FakeItEasy
Asked Answered
G

1

6

When i try to fake a delegate-type, i get System.InvalidCastException

[TestMethod]
public void TestDelegateFake()
{
    var func = A.Fake<Func<long, object>>();

    A.CallTo(() => func(A<long>.Ignored))
        .Returns(new {});

    func(123);
}

How du i fake delegates?

Godber answered 17/7, 2012 at 8:21 Comment(0)
C
8

I think you have to specify the Invoke-method explicitly:

A.CallTo(() => func.Invoke(A<long>.Ignored)).Returns(new {});
Charteris answered 17/7, 2012 at 19:49 Comment(2)
I have fixed this in the latest version nuget.org/packages/FakeItEasy/1.7.4582.63Sebbie
The posted fix worked without the call to Invoke. I think that is more intuitive - thx.Godber

© 2022 - 2024 — McMap. All rights reserved.