views:

30

answers:

2

I'm learning TDD (in Javascript), and I wanted to know, what is the right way to use configuration variables? Should I make a separate class and have them be member variables of the class, and pass an instance of the class to every object that needs it, or make a list of global variables and just use those? What are the benefits / setbacks of each method?

For example, I have to get data from a URL as follows:

function getData (remoteDataDelegate) {
    remoteDataDelegate.getData(userInfoURL)
}

where userInfoURL is a configuration variable that I set elsewhere to the URL for a page on my site.

A: 

Global variables are usually set by the setUp method of your TestCase.

S.Lott
A: 

This is an example of how I do it:

function NewConfiguration() {
    var config = {};
    config.carriersSelector = NewCarriersSelector();
    config.paymentMethodsSelector = NewPaymentMethodsSelector();

    return config;
}

Usage:

function NewOrderModel(request, searchRequest) {
    var configuration = NewConfiguration();
    // ... other variables code
var that = {
    getContentSuccess: function(cb) {
        // .. setup code

        $.ajax({
            type:       'GET',
            url:        request.page,
            dataType:   'json',
            data:       request.data,
            async:      request.async,
            success:    function(data) {
                if (data.status === 'success') {
                    cb(data.html, activeCustomer, step, configuration);
                }
                if (data.status == 'flash') {
                    flash(data.flash);
                }
            },
            complete:   request.complete
        });
    },

}

You will notice that the configuration is not being injected. for me, in this sample code the configuration never changes. Now my request objects change so they get injected, so I can mock those out or redirect the pages.

Gutzofter