none
Unit Test for ShimDataTableCollection Count

    Question

  • Hi, I'm trying to use Microsoft Fakes' ShimDataSet and ShimDataTableCollection for my unit tests.  Everything works fine with the following codes:

    using (ShimsContext.Create()
    {              
      // Faking Fill
      ShimDbDataAdapter.AllInstances.FillDataSetString = (a, b, c) => 0;

      // Faking Adapter, from command for current instance of command
      ShimSqlDataAdapter.ConstructorSqlCommand = (@this, value) => new ShimSqlDataAdapter(@this);

      // fake Data Set
      ShimDataSet.Constructor = (@this) => new ShimDataSet(@this)
      {
        // fake DataTableCollection of data set
        TablesGet = () => new ShimDataTableCollection()
        {
          ItemGetInt32 = (S) => {
            var dt = new DataTable("TABLE");
            dt.Columns.Add("c1");
            dt.Columns.Add("c2");
            dt.Rows.Add("ColumnValue:c1", "ColumnValue:c2");
            return dt;
          }
        },
        // fake culture set for data set
        LocaleSetCultureInfo = (c) => { }
      };
    }

    However, now in my product codes, I start referencing the DataSet.Tables.Count and I can't find a way to shim the Count property (e.g. CountGet is not found with ShimDataTableCollection)

    Any suggestion to get this going?

    Aaron

    • Moved by Fred Bao Friday, October 03, 2014 6:52 AM Unit Test question
    Thursday, October 02, 2014 11:22 PM

All replies

  • Hi AK Ching,

    I'm afraid that it is not the correct forum about this issue, since this forum is to discuss CLR.

    So I have moved this thread to Visual Studio Unit Testing forum for better response.

    Thanks for your understanding.

    Best regards,
    Franklin


    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.

    Friday, October 03, 2014 6:53 AM
  • Hi Aaron,

    Could you provide detail code to me or share the project on the OneDrive?

    Best Regards

    Starain Chen


    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.

    Friday, October 03, 2014 9:27 AM
    Moderator
  • Starain,

    In the product codes, basically we call SqlDataAdapter to execute a SqlCommand, fill a DataSet with the returned DataTable and check if the DataTable.Tables.Count is > 0.  Here is the simplified codes:

                    using (var cn = new SqlConnection(connectionString))
                    {
                        cn.Open();

                        using (var ds = new DataSet())
                        {
                            ds.Locale = System.Globalization.CultureInfo.InvariantCulture;
                            using (var cmd = new SqlCommand(sqlStatement))
                            {

                                using (var adapter = new SqlDataAdapter(cmd))
                                {
                                    cmd.Connection = cn;
                                    adapter.Fill(ds, "TABLE");
                                    if (ds.Tables.Count > 0)

                                    {

                                        return doSomethingToReturnTheResult(ds.Tables[0]);

                                     }

                                     else

                                     {

                                        return null;

                                      }

                                }
                            }
                        }
                    }

    To unit test these codes, we fake System.Data and with these test codes:

                using (ShimsContext.Create())
                {
                    ShimSqlConnection.ConstructorString = (c, s) => { /* Do nothing **/};           
                    ShimSqlConnection.AllInstances.Open = (c) => { /* Do nothing **/};
                    ShimSqlConnection.AllInstances.Close = (c) => { /* Do nothing **/};
                    ShimSqlDataReader.AllInstances.Close = (c) => { /* Do nothing **/};   

                
                    // Faking Fill
                    ShimDbDataAdapter.AllInstances.FillDataSetString = (a, b, c) => 0;
                   
                    // Faking Adapter, from command for current instance of command
                    ShimSqlDataAdapter.ConstructorSqlCommand = (@this, value) => new ShimSqlDataAdapter(@this);
                   
                    // fake Data Set
                    ShimDataSet.Constructor = (@this) => new ShimDataSet(@this)
                    {
                        // fake DataTableCollection of data set
                        TablesGet = () => new ShimDataTableCollection()
                        {
                            ItemGetInt32 = (S) => {
                                var dt = new DataTable("TABLE");
                                dt.Columns.Add("c1");
                                dt.Columns.Add("c2");
                                dt.Rows.Add("ColumnValue:c1", "ColumnValue:c2");
                                return dt;
                            }
                        },
                       
                        // fake culture set for data set
                        LocaleSetCultureInfo = (c) => { }
                    };

                    // Act

                    var result = DoSomethingToHitProductCode();

                    // Assert

                    Assert.IsNotNull(result);

    Friday, October 03, 2014 4:31 PM
  • Hi Aaron,

    Is it possible to create a subclass of that type to fake that?

    Allen

    Tuesday, November 18, 2014 8:47 AM