0

I need to test this class:

public abstract class GaBase
{
    protected GoogleAnalyticsInfo GAInfo;

    protected abstract void PopulateGAInfo();

    public string GetGoogleAnalyticsTag()
    {
        //Return any info related to GAInfo
    }

    //Some other stuffs
}

I need to unit test the GetGoogleAnalyticsTag method, but I need to set the property GAInfo to test it properly. In a production code, we do that using my PopulateGaInfo method when we derive from this class.

How can I set GAInfo using stubs?

Here is my test method:

public void MyTest1()
{          
    var ga =  new StubGaBase()
        {
            PopulateGAInfo01 = () =>
                {
                    // How can I set GAInfo here?
                }
        };

    // The method I need to test
    var script = ga.GetGoogleAnalyticsTag();

    // My asserts
}
Ryan Gates
  • 4,501
  • 6
  • 50
  • 90
Kapoue
  • 847
  • 2
  • 11
  • 15
  • 1
    The awnser depends on what you want to test and working of the GetGoogleAnalyticsTag() method. Without implementation it is realy hard to awnser your question. – Peter Apr 17 '13 at 11:49
  • If you would like to be known/addressed as Benjamin, please update your profile rather than putting that information in your question. – Ryan Gates Jun 06 '13 at 14:15

1 Answers1

0

There is a pattern known as "Subclass-to-Test" which suggests if you need access to methods or behaviors (such as simulating raising events) you would hand-roll a stub/mock that exposes the functionality you need for the test. This stub exists only in your test project.

if you are only interested in testing protected variables of the abstract class, simply create a derived class and expose a method that performs the work and returns the values you are interested in.

bryanbcook
  • 16,210
  • 2
  • 40
  • 69