9

I am trying to build a test against some legacy method that implement out parameters. Could you give me an example how to do this?

Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
user9969
  • 15,632
  • 39
  • 107
  • 175
  • 2
    You need to clarify what you're doing, and why that doesn't work as expected. Just "Moq with out parameters" isn't enough - to start with, why do you need Moq? Please show some code. – Tomas Aschan Jun 15 '10 at 08:41
  • Fair enough.I have to make something up as the enviroment is complicated.I just wondered if there was a complete snippet somewhere.That's all. I will put something together and post it – user9969 Jun 15 '10 at 08:44

3 Answers3

24

Just assign the out or ref parameter from the test.

Given this interface:

public interface ILegacy
{
    bool Foo(out string bar);
}

You can write a test like this:

[TestMethod]
public void Test13()
{
    string bar = "ploeh";

    var legacyStub = new Mock<ILegacy>();
    legacyStub.Setup(l => l.Foo(out bar))
        .Returns(true);

    Assert.IsTrue(legacyStub.Object.Foo(out bar));
    Assert.AreEqual("ploeh", bar);
}
Paul Johnson
  • 1,417
  • 1
  • 17
  • 26
Mark Seemann
  • 225,310
  • 48
  • 427
  • 736
  • fantastic.Thats all I needed .Just something to get me going. One thing though you didnt use the "It.IsAny" stuff EG (Foo(out it.IsAny)); I thought it was some sort of required.Could you clarify I would be grateful.thanks – user9969 Jun 15 '10 at 08:55
  • Lots of It.Is* in the QuickStart too... - I reccomend regular reading of same - you wont be able to digest it in a single pass (and you're doing something wrong if you need it all in a single test suite!) – Ruben Bartelink Jun 15 '10 at 09:02
  • 1
    @devnet247: How is this not worth of a +1 from you - it shows little respect for Mark's time? (Someone gave the Q a +1 too - I assume it's Mark - I have it a -1 to counter it as no homework done). +1ing this answer. – Ruben Bartelink Jun 15 '10 at 09:04
  • hi Ruben.I have marked 2 which is the max I seem to be allowed to do I had not looked at the 1 sign yet. – user9969 Jun 15 '10 at 09:10
  • This could really use some clarity about the differences of ref and out as they are totally different to Moq. The question is targeting out but I would hate for someone to interpret ref the same way. – Morgeth888 Apr 20 '17 at 20:40
2

Anything wrong with the second example at the top of https://github.com/moq/moq4/wiki/Quickstart ? You really should be giving examples of what you're trying to do if you're not going to look for things like this.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
Ruben Bartelink
  • 59,778
  • 26
  • 187
  • 249
  • I agree.I apologise.I really didnt know where to start on this one as the actual "Real Code" was a bit complicated and could not post it.I should have build a noddy example myself.Lesson learned – user9969 Jun 15 '10 at 09:01
1

Incidentally if you want to use moq (currently) to mock the out parameter too you'll also have to do the following hoop jump. Lets say that you wanted to mock an out parameter that returned another mocked object e.g.

var mockServiceA = new Mock<IMyService>();
var mockServiceOutput = new Mock<IMyServiceOutput>();

// This will not work...
mockServiceA.Setup(svc => svc.DoSomething(out mockServiceOutput.Object));

// To have this work you have to do the following
IMyServiceOutput castOutput = mockServiceOutput.Object;
mockServiceA.Setup(svc => svc.DoSomething(out castOutput));
The Senator
  • 5,181
  • 2
  • 34
  • 49