4

I have the following Func method which i need to mock off

Func<Owned<ISomeInterface>> someMethod { get; set; }

but cant figure out how to mock it off using 'Moq' framework.

I have read a similar post on SO but still cant seem to mock it off, it always comes back with

Expression is not a method invocation: x => Invoke(x.someMethod )

or

A matching constructor for the given arguments was not found on the mocked type. ----> System.MissingMethodException : Constructor on type 'Owned`1Proxy40a9bf91815d4658ad2453298c903652' not found.

Community
  • 1
  • 1
  • 1
    I have read a related post on stackoverflow –  Jan 06 '12 at 09:27
  • Does that mean this question should be closed as a duplicate? Please clarify if this question is resolved.. – MattDavey Jan 06 '12 at 09:33
  • 1
    sorry, the post did not seem to help me as it didn't work for me. I meant to edit the question but accidentally added a comment. I have now updated the question –  Jan 06 '12 at 09:35
  • why mock this Func? This is just a property and you can set it to any method which satisfies the signature and then call it, can't you? – Helikaon Jan 06 '12 at 09:59
  • @Helikaon, in my case, I wish to mock a sequence so the method returns different results when called multiple times. I'd also like to verify the number of times it is called. – Doug Domeny Mar 16 '16 at 13:06
  • Yes, but u have full control over ur delegate method implementation. What about if(callCount == 1) return value1; if(callCount==2) return value2; and u can closure a local callCount variable in ur test – Helikaon Mar 16 '16 at 13:12

1 Answers1

4

The Funct is defined as a property so you should use SetupSet within Moq

public interface IPersona
{
    string nome { get; set; }
    string cognome { get; set; }
    Func<Owned<ISomeInterface>> somemethod { get; set; }

}

. In your test :

You create a mock for the Func:

Func<Owned<ISomeInterface>> somemethodMock = () => new Mock<Owned<ISomeInterface>>().Object; 

THen you setup the mock for the Class containing the Func as a property and you setup the expectation on the Set method :

var obj = new Mock<IMyInterface>();
obj.SetupSet(x => x.somemethod = somemethodMock).Verifiable();

You create the container object for the mock:

//We pass the mocked object to the constructor of the container class
var container = new Container(obj.Object);
container.AnotherMethod(somemethodMock);
obj.VerifyAll();

Here is the definition of Another method of the Container class, if get the func as an input parameter and set it to the property of the contained object

enter  public class Container
{
    private IPersona _persona;

    public Container(IPersona persona)
    {
        _persona = persona;
    }

    public void AnotherMethod(Func<MyClass<IMyInterface>> myFunc)
    {
        _persona.somemethod = myFunc;
    }      
}
Giorgio Minardi
  • 2,765
  • 1
  • 15
  • 11
  • 1
    Thanks, this seems to have worked. I am now stuck on the very next line of the code lol where i have a static method being called from another assembly... will it ever end ;-) –  Jan 06 '12 at 15:13
  • That is painful :) Have a look at this: http://blogs.clariusconsulting.net/kzu/how-to-mock-extension-methods/ it covers extension methods but it should fit considering they're static, hope it helps. – Giorgio Minardi Jan 06 '12 at 15:27
  • 1
    AutofacContrib.Moq.dll allows you to do a lot with Mocking off Funcs etc. and seems to have done the trick –  Jan 19 '12 at 11:27