tags:

views:

1330

answers:

2

Is it possible to assign an out/ref parameter using Moq (3.0)?

I've looked at using Callback(), but Action<> does not support ref parameters because it's based on generics. I'd also preferably like to put a constraint (It.Is) on the input of the ref parameter, though I can do that in the callback.

I know that Rhino Mocks supports this functionality, but the project I'm working on is already using Moq.

+2  A: 

Seems like it is not possible out of the box. Looks like someone attempted a solution

See this forum post http://code.google.com/p/moq/issues/detail?id=176

this question http://stackoverflow.com/questions/726630/verify-value-of-reference-parameter-with-moq

Gishu
Thanks for the confirmation. I had actually found those two links in my searching, but also noticed that Moq lists one of it's features as "supporting ref/out parameters", so I wanted to be sure.
Richard Szalay
+3  A: 

For 'out', the following seems to work for me.

public interface IService
{
    void DoIt(out string a);
}

[TestMethod]
public void Test()
{
    var service = new Mock<IService>();
    var a = "output value";
    service.Setup(s => s.DoIt(out a));

    string b;
    service.Object.DoIt(out b);
    Assert.AreEqual("output value", b);
}

I'm guessing that Moq looks at the value of 'a' when you call Setup and remembers it.

For 'ref', I'm looking for an answer also.

I found the following QuickStart guide useful: http://code.google.com/p/moq/wiki/QuickStart

Parched Squid
I think the problem I had was that where is no method of _assigning_ the out/ref params from the method `Setup`
Richard Szalay
I don't have a solution for assigning a ref parameter.This example does assign a value of "output value" to 'b'. Moq doesn't execute the Expression you pass to Setup, it analyzes it and realizes that you are providing 'a' for an output value, so it looks at the present value of 'a' and remembers it for subsequent calls.
Parched Squid