'How to Mock QueryAsync using Dapper for unit testing an Oracle stored procedure call which returns a RefCursor

I am writing a unit test case using MOQ framework for testing an Oracle stored procedure returning a RefCursor.

My project is in c# and we are using Dapper to connect to Oracle DB.

This is the code am using to Mock response of QueryAsync in my test case.

IEnumerable<dynamic> responseData = new List<dynamic>() { responseResult };
var query = @"TestProcedure";
_connectionFactory.Setup(x => 
    x.GetOpenConnection()).Returns(_dbConnection.Object);
_dbConnection.SetupDapperAsync(c => c.QueryAsync(query, It.IsAny<OracleDynamicParameters>(), It.IsAny<DbTransaction>(), It.IsAny<int>(), 
It.IsAny<CommandType>())).ReturnsAsync(responseData);

This is the piece of code am hoping to test.

var param = new OracleDynamicParameters();
param.Add("p1", ProductLineCode, OracleMappingType.Varchar2, ParameterDirection.Input, size: 2000);
param.Add("p2", dbType: OracleMappingType.RefCursor, direction: ParameterDirection.Output);

var result = await connection.QueryAsync("TestProcedure", param, commandType: CommandType.StoredProcedure); 

While debugging the test case I encounter the below error.

System.NotSupportedException: 'Whoopsies! This library will only work with Oracle types, you are attempting to use type Castle.Proxies.DbParameterProxy.'

Need help in resolving the above issue



Solution 1:[1]

This is not the answer you want, but I strongly urge you not to do that. Your test will have no value, other than maybe increasing your code coverage a bit.

If your database layer code is written right, there will be no testable logic inside them, everything will depend on Dapper and Oracle and both have been thoroughly tested.

You should instead hide your whole database access behind interfaces and mock those in your business logic, which is what you really want to test.

For further reading see this and this paper by Jim Coplien.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Palle Due