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

Mocking OAConnection and OACommand using OpenAccess ORM

3 Answers 61 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Harlan
Top achievements
Rank 1
Harlan asked on 08 Nov 2012, 06:23 PM
Hi,
I am using OAConnection and OACommand to return multiple result sets.
This is working fine. However, I am trying to unit test my code and am having problems mocking them.
I would rather not have to actually connect to the database, I just want to mock the connections and dbDataReader to return pre defined values set by mocking. If the only way to do this is to connect to the database I will do that, but I would still want to mock call for the stored proc.
I think I can use IngnoreInstance to mock the calls, but I am having two issues.
The first issue, is that I am not able to mock the OAConnection.
I might be able to get around this my setting the the connection info in the App.Config file and create an actual instance of the Entity model.
Of course this wil create an actual connection to the database, but I will do it if there is no other way.
Then I hope to be able to mock the OACommand and dbDataReader .

The second issue is that I am not sure how to mock the OACommand and dbDataReader .
Heres is a sample of the code I am trying to mock. Any help would be greatly appreciated.

EntitiesModel dbContext = new EntitiesModel();
if (dbContext != null)
{
    EntityName myEntity = EntityName();
         
    using (OAConnection connection = dbContext.Connection)
    {
        using (OACommand command = connection.CreateCommand())
        {
             command.CommandType = System.Data.CommandType.StoredProcedure;
             command.CommandText = "SpName";
 
             using (DbDataReader dataReader = command.ExecuteReader())
             {
                  if (dataReader.HasRows)
                  {
                       IEnumerable<EntityName> entityname = dbContext.Translate<EntityName>(dataReader);
                       foreach EntityName c in entityname
                       {
                           myEntity = dbContext.AttachCopy<EntityName>(c);
                       }
 
                  }
 
                 dataReader.NextResult();
                 if (dataReader.HasRows)
                 {
                      while (dataReader.Read())
                      {
                           this._myField = dataReader.GetString(1);
                       }
                  }
                  if (!dataReader.IsClosed)
                      dataReader.Close();
             }
        }
    }
 }

3 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 13 Nov 2012, 10:10 PM
Hi Harlan,

Thanks again for contacting us. My colleague Kaloyan has created a sample where he has shown mocking both OACommand and OADataReader. Please check it out.

However, we noticed that there is an assembly signing issue in the latest release and should need a fix for that then please open a support ticket where we will send you the updated build.

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Harlan
Top achievements
Rank 1
answered on 13 Nov 2012, 11:29 PM
Thank you for the code example.
I did not explain it well in my example and I apologize for the confusion, but I was trying to mock the objects in the Entity model so I can pass it to a method to test the Method. Below is an example that I hope will show what I am trying to do. Also the Mocking code that I am trying, but is not working.
Again, I apologize for the confusion.

Here is the Code to be tested.

class CarClass
{
public Car CnnCmdReader(dbContext)
{
Car myCar = new Car();
if (dbContext != null)
{
using (OAConnection connection = dbContext.Connection)
{
using (OACommand command = connection.CreateCommand())
{
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "SpName";
using (DbDataReader dataReader = command.ExecuteReader())
{
if (dataReader.HasRows)
{
IEnumerable<Car> carName = dbContext.Translate<Car>(dataReader);
foreach (Car c in carName)
{
myCar = dbContext.AttachCopy<Car>(c);
}
}
if (!dataReader.IsClosed)
dataReader.Close();
}
}
}
}
return myCar
}
}


And here is the test code.

public void TestCnnCmdReader()
        {
             
            SystemRepository targetCarClass = Mock.Create<CarClass>();
            Car expected = null
            Car actual = null
 
            var dbContext = Mock.Create<EntitiesModel>();
            var oaConnectionMock = Mock.Create<OAConnection>(Constructor.Mocked);
            var oaCommand = Mock.Create<OACommand>();
            var oaReader = Mock.Create<OADataReader>();
 
            Mock.Arrange(() => dbContext.Connection).Returns(oaConnectionMock);
            Mock.Arrange(() => oaConnectionMock.Open()).DoNothing();
            Mock.Arrange(() => oaConnectionMock.CreateCommand()).Returns(oaCommand);
            Mock.Arrange(() => oaCommand.ExecuteReader()).Returns(oaReader);
            Mock.Arrange(() => oaReader.HasRows).Returns(true);
            Mock.Arrange(() => dbContext.Translate<Car>(oaReader)).ReturnsCollection(FakeCollection());
            Mock.Arrange(() => dbContext.AttachCopy<Car>(Arg.IsAny<Car>())).DoNothing().MustBeCalled();
 
            actual = targetCarClass.CnnCmdReader(dbContext)      
  
            Mock.Assert(actual != expected);
 
      }


0
Ricky
Telerik team
answered on 15 Nov 2012, 11:51 PM
Hi Harlam,
Thanks again for your sample. However taking a quick look at your sample, I found the following line:

Mock.Assert(actual != expected);

Mock.Assert is for validating expectations that you have in Mock.Arrange. It accepts either the mock object that you want to validate or a lambda for the method that you expect to be called. For asserting values, please use the built in Assert in test frameworks.


In addition, I will further investigate your sample and write you back here. 

 

Kind Regards
Ricky
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
General Discussions
Asked by
Harlan
Top achievements
Rank 1
Answers by
Ricky
Telerik team
Harlan
Top achievements
Rank 1
Share this question
or