Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mock an OracleConnection and OracleCommand?

For my tests I need to mock out the data client, in my case they are Oracle.

I have created my data access layer to allow this to be passed in:

public static Int32? GetUserRoleId(string userName, OracleConnection cn, OracleCommand cmd)

I am using Moq, though I can switch to another framework if needed, and when I go to create the Mock objects like this:

Mock<OracleConnection> mockOracleConnection = new Mock<OracleConnection>();
Mock<OracleCommand> mockOracleCommand = new Mock<OracleCommand>();

I get this error:

Failure: System.ArgumentException : Type to mock must be an interface or an abstract or non-sealed class.

Conclusion: This was more simple than I thought! Just mock the DAL layer function like this:

mockDao.Setup(a => a.GetUserRoleId(userName, It.IsAny<OracleConnection>(), It.IsAny<OracleCommand>())).Returns(1);
like image 803
Lucas B Avatar asked Sep 16 '10 15:09

Lucas B


1 Answers

You can make changes to use IDbConnection and IDbCommand (use interfaces and have a factory to provide the real objects in main code and mock objects in test - normally using Dependency Injection)

Moq can only mock Interfaces and virtual methods.

like image 161
Aliostad Avatar answered Oct 16 '22 18:10

Aliostad