How can I mock OracleConnection and OracleCommand?

For my tests, I need to mock the data client, in my case it's Oracle.

I created my level of data access so that this can be transmitted:

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

I use Moq, although I can switch to another framework if necessary, and when I go to create Mock objects like this:

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

I get this error:

Disclaimer: System.ArgumentException: The type for bullying must be an interface or an abstract or unsealed class.

Conclusion: It was easier than I thought! Just make fun of the function of the DAL layer as follows:

 mockDao.Setup(a => a.GetUserRoleId(userName, It.IsAny<OracleConnection>(), It.IsAny<OracleCommand>())).Returns(1); 
+6
c # oracle unit-testing moq
source share
2 answers

You can make changes to the use of IDbConnection and IDbCommand (use interfaces and factory to provide real objects in the main code and the layout of objects in the test - usually using dependency injection)

Moq can only fake interfaces and virtual methods.

+12
source share

You are trying to make fun of a closed class: you can look here .

By the way: as @Aliostad said , such a structure - most of the falsified frameworks I've seen - can only make fun of interfaces / abstract classes.

0
source share

All Articles