views:

68

answers:

3

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.

+9  A: 

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.

Aliostad
A: 

You're trying to mock a sealed class: you can look at here.

BTW: As @Aliostad said, such framework - most mock frameworks I've seen too - can mock only Interfaces/Abstract classes.

Markon
A: 

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);
Lucas B
While that may be the solution to your problem, this isn't a valid answer to the question that was asked. It looks like what you wanted to ask was, "How can I stub a call to a method that accepts an OracleConnection and OracleCommand as parameters?"
Kiff
@Kiff good point, unfortunately, I didn't know what I needed to ask. Usually the problem, right?! So, while the other answers are "correct" for the question asked, they didn't resolve my problem. I think I should remove this answer and provide an edit/update at the bottom of my question with this resolution. Do you or others agree?
Lucas B