chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
can anyone help me mock this method (MyMethod) in this class:
I an using the Enterprise Library and want to mock my Data Access Layer.

public class MyClass
private string sqlCommand;
private DbCommand dbCommand;
private Database db;
private IDataReader dataReader;


public MyObject MyMethod(string name)
MyObject myObject = null;

db = DatabaseFactory.CreateDatabase();

sqlCommand = "StoredProcedure";
dbCommand = db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(dbCommand, "Name", DbType.String, name);

using (IDataReader dataReader = db.ExecuteReader(dbCommand))
if (dataReader.Read())
myObject = new MyObject();
myObject.Id = Convert.ToInt32(dataReader["Id"]);
myObject.Name = dataReader["Name"] as string;
myObject.Description = dataReader["Description"] as string;

return myObject;
asked by mzelinski (2.2k points)

3 Answers

0 votes
This really depends on what you want to mock, here is an example:

Suppose we want the first call to MyClass.MyMethod to return a 'testObject'

Mock mockMyClass = MockManager.Mock(typeof(MyClass));

now the following code will return 'testObject'
MyClass theClass = new MyClass();
MyObject theObject = theClass.MyMethod("whatever");

More information can be found in the help included in the installation and here
answered by scott (32k points)
0 votes
I should have been a little more explicit.
i want to mock each call inside the method that calls the database.
for example
Mock dbfMock = MockManager.Mock(typeof(DatabaseFactory));

MockObject dbMock = MockManager.MockObject(typeof(SqlDatabase), Constructor.NotMocked, "ConnectionString");
Database db = (Database)dbMock.Object;
dbfMock.ExpectAndReturn("CreateDatabase", db, 1);

MockObject dbCommandMock = MockManager.MockObject(typeof(SqlCommand));
DbCommand dbCommand = (DbCommand)dbCommandMock.Object;
dbMock.ExpectAndReturn("GetStoredProcCommand", dbCommand).Args("StoredProcedure");

is as far as i have gotten.
its inside and including the using where im having problems

answered by mzelinski (2.2k points)
0 votes
Probably something like this:
// create an IDataReader Mock object and return it when ExecuteReader is called
MockObject mockIDataReader = MockManager.MockObject(typeof(IDataReader));
db.ExpectAndReturn("ExecuteReader",mockIDataReader as IDataReader);

mockIDataReader.ExpectAndReturn("Read",true);  // (Note 1)
// send dummy data (Note 2)
// called by using 
mockIDataReader.ExpectCall ("Dispose");

Note 1: If we want to check a bad Read we can use

Note 2: If we want to create a test that is more rigid to changes in the method and will allow us to change the order of the MyObject initilization block without failing the tests, we can use conditional expectations (This requires an Enterprise License)
// send dummy data
answered by scott (32k points)