3

I'm using moq and would like to mock a method which accepts out parameter:

protected void GetDataRow(string id, out DataRow dataRow)

This is what I tried:

dataMock.Protected().Setup("GetDataRow", ItExpr.IsAny<string>(), ItExpr.IsAny<DataRow>());

However, it returns:

System.ArgumentException : Member DataManager.GetDataRow does not exist.

If I change the dataRow parameter not to be out, everything works as expected.

How should I create a mock in this case?

Johnny
  • 8,939
  • 2
  • 28
  • 33
Bart
  • 294
  • 1
  • 2
  • 10

2 Answers2

4

You cannot use IsAny<DataRow> with out parameter. Instead of the IsAny<DataRow> you can create local DataRow variable and pass that one to the mock. The call of the mock later on will return dataRow as is, so you can use that one to verify your test expectations.

DataRow dataRow = new DataRow();
dataMock.Protected().Setup("GetDataRow", ItExpr.IsAny<string>(), out dataRow);
Johnny
  • 8,939
  • 2
  • 28
  • 33
  • 1
    Thank you, but this does not seem to work, I'm getting: error CS1615: Argument 3 may not be passed with the 'out' keyword – Bart Dec 14 '16 at 21:55
1

It can be done since moq 4.8.0-rc1 (2017-12-08). You can use ItExpr.Ref<DataRow>.IsAny for match any value for ref or out parameters. In your case:

dataMock.Protected().Setup("GetDataRow", ItExpr.IsAny<string>(), ItExpr.Ref<DataRow>.IsAny);
itaiy
  • 1,152
  • 1
  • 13
  • 22