chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hi,

I have a situation as follows:

public abstract BaseClass
{
  public static DataSet ExecuteExpensiveSqlCall(SqlParams sp){
    ...
  }
}

public ExtendingClass : BaseClass
{
   public static DataSet MethodIWantToTest(string param)
   {
       ... //build up parameters
       DataSet returnMe = ExecuteExpensiveSqlCall(sp);
       ...//do something with result
       return returnMe;
   }
}


I want to test that the MethodIWantToTest does what I want it to do with the DataSet returned by ExecuteExpensiveSqlCall, but without actually executing the expensive SQL call. Any suggestions on how to mock the ExecuteExpensiveSqlCall with TypeMock? I'd also like to have the mocked ExecuteExpensiveSqlCall return a stubbed dataset I create in my test, so that I can verify the result as such:

public void MyTest()
{ 
  DataSet stubDataSet = new DataSet()
  ...//build up stub dataset
  ...//set up mocks
  mock.ExpectAndReturn("ExecuteExpensiveSqlCall", stubDataSet, typeof(DataSet));
  DataSet actual = ExtendingClass.MethodIWantToTest("ignored");
  Assert.AreEqual("expected value", actual.Tables[0].Rows[0]["my_column"]);
}


Any help appreciated.

Thanks,
Nick
asked by zcrar70 (2.9k points)

2 Answers

0 votes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using TypeMock;
using NUnit.Framework;

namespace Example
{
    public abstract class BaseClass
    {
        public static DataSet ExecuteExpensiveSqlCall(string sqlParams)
        {
            //return DataSet from database
            return null;
        }
    }

    public class ExtendingClass : BaseClass
    {
        public static DataSet MethodIWantToTest(string param)
        {
            //build up parameters
            DataSet returnMe = ExecuteExpensiveSqlCall(param);
            //do something with result
            return returnMe;
        }
    }

    public class TestClass
    {
        [Test, VerifyMocks]
        public void MyTest()
        {
            DataSet stubDataSet = new DataSet();
            //build up stub dataset
            //set up mock on extending object
            Mock baseMock = MockManager.MockAll<BaseClass>();
            //expect method call
            baseMock.ExpectAndReturn("ExecuteExpensiveSqlCall", stubDataSet);
            DataSet stubRetDataSet = ExtendingClass.MethodIWantToTest("ignored");
            //complete assertions
        }
    }
}
answered by bradgearon (1.5k points)
0 votes
Thanks :)
answered by zcrar70 (2.9k points)
...