Joe Davis
Joe Davis

Reputation: 233

Moq is not respecting OUT parameters

I am trying to use Moq to mock a method with two OUT parameters. This should work according to the Moq samples here: https://code.google.com/p/moq/wiki/QuickStart

var moqDB = new Mock<IMyDB>();

int Value1 = 500000;
decimal Value2 = 0.2M;

moqDB.Setup(db => db.DoSomething(out Value1, out Value2)).Returns(true);

But it does not set the values within the method I'm testing:

public virtual void TestMethod(IMyDB db)
{
    int Value1 = 0;
    decimal Value2 = 0.0M;

    db.DoSomething(out Value1, out Value2);

    // Check Values
}

What am I doing wrong?

Upvotes: 0

Views: 445

Answers (1)

evanmcdonnal
evanmcdonnal

Reputation: 48114

moq doesn't actually change the out values. In the example you posted they initialize the out value to the value they want (var outString = "ack";). In your code you start them at 0 and expect the function to return the proper values.

I don't see any documents that indicate you can moq out values. Instead your code should just be;

public virtual void TestMethod(IMyDB db)
{
    int Value1 = 500000;
    decimal Value2 = 0.2M;

    db.DoSomething(out Value1, out Value2);

    // look the out values are 500000 and .2M, OMG!!!
 }

kind of lame...

Upvotes: 1

Related Questions