Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moq Example using out and ref needed

Tags:

moq

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?

like image 342
user9969 Avatar asked Jun 15 '10 08:06

user9969


2 Answers

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);
}
like image 75
Mark Seemann Avatar answered Jan 02 '23 02:01

Mark Seemann


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.

like image 29
Ruben Bartelink Avatar answered Jan 02 '23 00:01

Ruben Bartelink