Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7760121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:47:59+00:00 2026-06-01T13:47:59+00:00

I googled on how to unit test but examples are so simple. the examples

  • 0

I googled on how to unit test but examples are so simple. the examples always show functions that return something or do ajax that returns something – but never have i seen examples that do callbacks, nested callbacks and functions that are “one-way”, that they just store something and never return anything.

say i have a code like this, how should i go about testing it?

(function(){
    var cache = {};

    function dependencyLoader(dependencies,callback2){
        //loads a script to the page, and notes it in the cache
        if(allLoaded){
            callback2()
        }
    }

    function moduleLoader(dependencies, callback1){
        dependencyLoader(dependencies,function(){
            //do some setup
            callback1()
        });
    }

    window.framework = {
        moduleLoader : moduleLoader
    }

}());


framework.moduleLoader(['foo','bar','baz'],function(){
    //call when all is loaded
})
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T13:48:01+00:00Added an answer on June 1, 2026 at 1:48 pm

    This illustrates a problem with keeping things private in an anonymous function in javascript. It’s a bit difficult to validate that things are working internally.

    If this was done test first then the cache, dependencyLoader and moduleLoader should be publicly available on the framework object. Or else it would be difficult to validate that the cache was handled properly.

    To get things going I’d recommend you take a gander on BDD, that conveniently gives you an approach to help you start by letting you spell out the behaviour with a given-when-then convention. I like to use Jasmine, which is a javascript BDD framework (that integrates with jstestdriver), for this kind of thing and the unit tests I’d make for the sample you have above would be:

    describe('given the moduleloader is clear', function() {
    
        beforeEach(function() {
            // clear cache
            // remove script tag
        });
    
        describe('when one dependency is loaded', function() {
    
            beforeEach(function() {
                // load a dependency
            });
    
            it('then should be in cache', function() {
                // check the cache
            });
    
            it('then should be in a script tag', function() {
                // check the script tag
            });
    
            describe('when the same dependency is loaded', function() {
    
                beforeEach(function () {
                    // attempt to load the same dependency again
                });
    
                it('then should only occur once in cache', function() {
                    // validate it only occurs once in the cache
                });
    
                it('then should only occur once in script tag', function() {
                    // validate it only occurs once in the script tag
                });
    
            });
        });
    
        // I let the exercise of writing tests for loading multiple modules to the OP
    
    }); 
    

    Hope these tests are self explanatory. I tend to rewrite the tests so that they nest nicely, and usually the actual calls are done in the beforeEach functions while the validation are done in the it functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a couple of MFC applications. I don't want to unit-test UI but
Basically I'm trying to start some unit tests in google test but not sure
Has anyone been able to successfully unit test methods that are, by necessity, coupled
I'm programming some unit test with the Google test framework. But I want to
I'm using Google Test Framework to set some unit tests. I have got three
Googled everything, but can't find solution for my problem. When i'm trying to deploy
I googled for Drawing text on picturebox C# ,but I couldnt find anything useful.Then
I googled,I binged,I already have seen the other duplicates here,but none of them work
I googled this a little but couldn't find a good result. Right now I'm
I'm using the Boost::Test library for unit testing, and I've in general been hacking

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.