This is a migrated thread and some comments may be shown as answers.

Mock for Microsoft.Practices.EnterpriseLibrary.Data

1 Answer 63 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 2
James asked on 04 Apr 2014, 12:55 PM
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.

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.}


1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 09 Apr 2014, 10:44 AM
Hi James,

Thank you for contacting us.

As Database is an abstract class, you can directly create a mock of it, using the default Mock.Create<Database>() syntax. Keep in mind that, with the free version of JustMock, you will be able to arrange the expectations only for the virtual members of that mock. For example, there is no problem to set expectations against the GetStoredProcCommand function, like this:
[TestMethod]
public void ShouldMockDatabaseInstance()
{
    // Arrange
    var test = Mock.Create<Database>();
 
    Mock.Arrange(() => test.GetStoredProcCommand(Arg.AnyString)).IgnoreArguments().MustBeCalled();
 
    // Act
    test.GetStoredProcCommand("test");
 
    // Assert
    Mock.Assert(test);
}

However, if you want to arrange the AddInParameter function directly, you will need full JustMock. Then, you will be able to perform the following:
[TestMethod]
public void ShouldArrangeAgainstNonVirtualFunction()
{
    // Arrange
    var test = Mock.Create<Database>();
 
    Mock.Arrange(() => test.AddInParameter(Arg.IsAny<DbCommand>(), Arg.AnyString, Arg.IsAny<DbType>())).DoNothing().MustBeCalled();
 
    // Act
    test.AddInParameter(Mock.Create<DbCommand>(), "", new DbType());
 
    // Assert
    Mock.Assert(test);
}

I hope this helps. Please, let me know if I can help with anything else.

Regards,
Kaloyan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
JustMock Free Edition
Asked by
James
Top achievements
Rank 2
Answers by
Kaloyan
Telerik team
Share this question
or