none
How to mock SqlDataReader using Stubs or Shims RRS feed

  • Question

  • I tried to use stubs to mock IDataReader methods GetInt(int),GetString(int),GetGuid(int)which are used to read each column data from  SqlDataReader object.

    ex..

    Guid Id = inPropertyReader["Id"] as Guid? ?? Guid.Empty;
    int Number = inPropertyReader["Number"] as int? ?? 0;
    string Name = Convert.ToString(inPropertyReader["PriorityField"]);

    where inPropertyReader is IDataReader instance...

    Please help me in fixing this issue..

    --thanks in advance

    Regards,
    Raghu.
    • Moved by Fred BaoModerator Tuesday, November 4, 2014 7:41 AM Move to a more appropriate forum
    Monday, November 3, 2014 12:48 PM

Answers

  • At last i could shim the sqldatareader methods as below,

    ShimSqlDataReader.AllInstances.ItemGetString = (p1, p2) =>
                {
                    if (p2 == "OpportunityId")
                        return new Guid("352AFC98-D84E-4B4C-8EDD-542ED3A7FD22");
                    else if (p2 == "OpportunityNumber")
                        return 539;
                    else
                        return "Test";
                };

    thanks & Regards,

    Raghu.

    • Marked as answer by Raghu Masineni Wednesday, November 19, 2014 1:42 PM
    Wednesday, November 19, 2014 1:42 PM

All replies

  • Hello Raghu,

    >>Please help me in fixing this issue..

    Do you mean that you want to use the IDataReader instead of using the SqlDataReader? If it is, please check the demo example below:

    SqlConnection connection = new SqlConnection(@"Data Source=(localdb)\Projects;Database=DFDB;Trusted_Connection=True;");
    
                try
    
                {
    
                    connection.Open();
    
                    SqlCommand cmd = new SqlCommand("select OrderID,OrderName from [Order]", connection);
    
                    IDataReader inPropertyReader = cmd.ExecuteReader();
    
                    while (inPropertyReader.Read())
    
                    {
    
                        int ID = inPropertyReader.GetInt32(0);
    
                        string Name = inPropertyReader.GetString(1);
    
                    }
    
    
                }
    
                catch (Exception)
    
                {
    
    
                    throw;
    
                }
    
                finally
    
                {
    
                    connection.Close();
    
                }
    

    If I do not understand correctly, please feel free let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, November 4, 2014 8:03 AM
    Moderator
  • Hi Fred,

    Yes i am trying to mock IDataReader methods, so that i can get what ever the values needed from those methods.Please share us if there is any alternative to mock IDataReader ?

    Thanks In Advance,

    --Raghu

    Thursday, November 13, 2014 8:44 AM
  • I used stubs in the following way for mocking methods.But it did't work though it compiled successfully

    IDataReader inPropertyReader = new StubIDataReader()
                {

                    GetGuidInt32 = (p) => { return new Guid("352AFC98-D84E-4B4C-8EDD-542ED3A7FD22"); },
                    GetInt32Int32 = (p) => { return 539; },
                    GetStringInt32 = (p) => { return "Test"; },
                    ItemGetInt32 = (p) => { return 539; },
                };

    Opportunity opObj = new Opportunity(inPropertyReader);

    Regards,

    Raghu.

    Thursday, November 13, 2014 9:41 AM
  • At last i could shim the sqldatareader methods as below,

    ShimSqlDataReader.AllInstances.ItemGetString = (p1, p2) =>
                {
                    if (p2 == "OpportunityId")
                        return new Guid("352AFC98-D84E-4B4C-8EDD-542ED3A7FD22");
                    else if (p2 == "OpportunityNumber")
                        return 539;
                    else
                        return "Test";
                };

    thanks & Regards,

    Raghu.

    • Marked as answer by Raghu Masineni Wednesday, November 19, 2014 1:42 PM
    Wednesday, November 19, 2014 1:42 PM