Trying to find an example for mocking a repository that uses the Microsoft.Practices.EnterpriseLibrary.Data.
C# code. Pretty sure I can create the Unit Test, just cannot determine how to create a Mock for the Database object from Microsoft.Practices.EnterpriseLibrary.Data.
C# code. Pretty sure I can create the Unit Test, just cannot determine how to create a Mock for the Database object from Microsoft.Practices.EnterpriseLibrary.Data.
01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.Linq;
04.
using
System.Text;
05.
using
System.Threading.Tasks;
06.
using
Microsoft.Practices.EnterpriseLibrary.Data;
07.
using
System.Data;
08.
using
System.Data.Common;
09.
// Other using statements for Models
10.
11.
namespace
Test.Repository
12.
{
13.
public
class
MyDatabaseRepo : IMyDatabaseRepo
14.
{
15.
// Microsoft.Practices.EnterpriseLibrary.Data 'Database' object
16.
readonly
Database _database;
17.
18.
public
MyDatabaseRepo(Database dataSource)
19.
{
20.
_database = dataSource;
21.
}
22.
23.
public
int
InsertNewCustomer(ClientEntity client)
24.
{
25.
int
ClientRecordId = 0;
26.
using
(DbCommand dbCmd = _da.GetStoredProcCommand(
"usp_InsertNewCustomers"
))
27.
{
28.
_database.AddInParameter(dbCmd,
"@Address1"
, DbType.String, client.Address1);
29.
_database.AddInParameter(dbCmd,
"@Address2"
, DbType.String, client.Address2);
30.
_database.AddInParameter(dbCmd,
"@City"
, DbType.String, client.City);
31.
_database.AddInParameter(dbCmd,
"@State"
, DbType.String, client.State);
32.
_database.AddInParameter(dbCmd,
"PostalCode"
, DbType.String, client.PostalCode);
33.
_database.AddOutParameter(dbCmd,
"@recordID"
, DbType.Int32, 0);
34.
35.
using
(IDataReader reader = _database.ExecuteReader(dbCmd))
36.
{
37.
reader.Read();
38.
ClientRecordId = Convert.ToInt32(dbCmd.Parameters[
"@recordID"
].Value);
39.
}
40.
}
41.
42.
return
ClientRecordId;
43.
}
44.
}
45.
}