tags:

views:

28

answers:

2

Hi, 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?

Very grateful

+2  A: 

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);
}
Mark Seemann
fantastic.Thats all I needed .Just something to get me going.One thing though you didnt use the "It.IsAny" stuffEG (Foo(out it.IsAny<Bar>));I thought it was some sort of required.Could you clarify I would be grateful.thanks
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
@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
hi Ruben.I have marked 2 which is the max I seem to be allowed to doI had not looked at the 1 sign yet.
+1  A: 

Anything wrong with the second example at the top of http://code.google.com/p/moq/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.

Ruben Bartelink
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
+1 I kinda forgot about the quickstart too :)
Mark Seemann