0

I am using the following line in my code to execute the stored procedure,

var parameters = new DynamicParameters();
parameters.Add("MYPARAM", field.ListPickListCode, DbType.String, ParameterDirection.Input, int.MaxValue);

options = dbConn.QuerySingleOrDefault<string>(sql: "mysp", param: parameters, commandType: CommandType.StoredProcedure);

How should I set up my mock?

I tried below 2 mocking ways, but both failed,

var parameters = new DynamicParameters();
parameters.Add("MYPARAM", fieldDefinition.ListPickListCode, DbType.String, ParameterDirection.Input, int.MaxValue);

IDynamicDatabaseConnection dbConnMock = A.Fake<IDynamicDatabaseConnection>();
A.CallTo(() => dbConnMock.QuerySingleOrDefault<string>("mysp")).Returns("someValue");

And,

var parameters = new DynamicParameters();
parameters.Add("MYPARAM", fieldDefinition.ListPickListCode, DbType.String, ParameterDirection.Input, int.MaxValue);

IDynamicDatabaseConnection dbConnMock = A.Fake<IDynamicDatabaseConnection>();
A.CallTo(() => dbConnMock.QuerySingleOrDefault<string>("mysp", parameters, null, null, CommandType.StoredProcedure)).Returns("someValue");
Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
tRuEsAtM
  • 3,517
  • 6
  • 43
  • 83
  • How does it fail? Also I tried looking up IDynamicDatabaseConnection, but couldn't find this interface. Is this meant to be IDbConnection? – Hayden Dec 15 '22 at 00:12
  • IDynamicDatabaseConnection is a wrapper, inherited from IDbConnection. In the result, I expect a "someValue" string but instead I get empty string, "", and that's usually when provided SQL command doesn't match the actual SQL command. – tRuEsAtM Dec 15 '22 at 00:14

1 Answers1

1

As @Hayden says, we could likely help better if you supplied the errors. However, QuerySingleOrDefault appears to be an extension method, if I've found the right source. As noted in the documentation, FakeItEasy cannot override static methods (including extension methods). As has been answered elsewhere, if you find it necessary to fake an extension method (as opposed to refactoring your code to provide an alternative implementation), the closest approach can be to fake the methods that the extension methods exercise on the original interface. This works best for very simple extension methods, and can become quiet brittle for complicated ones.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • So there are no errors, if the Sql string I supplied doesn’t match with the one in actual code, then it returns blank string, “”, otherwise it returns the “someValue” – tRuEsAtM Dec 15 '22 at 16:03
  • Ah,, my mistake. But now I'm more confused. It sounds to me like it's working. match => return configured value, otherwise default value. What behaviour are you expecting? What is wrong? You're unlikely to get any responder who's intimately familiar with both Dapper and FakeItEasy, so the more you put into the question, the better. Ideally, a minimal reproduction, with explanation... – Blair Conrad Dec 16 '22 at 21:05