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 9257759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:12:42+00:00 2026-06-18T12:12:42+00:00

I’m using Backbone and Require.js. Everything works great but, I would like to add

  • 0

I’m using Backbone and Require.js. Everything works great but, I would like to add some unit tests to my application. I decided use Qunit.js.

In my main.js file I create new object EventsView:

require.config({
  paths: {
    jquery:                 'libs/jquery',
    underscore:             'libs/underscore',
    backbone:               'libs/backbone',
    qunit:                  'test/libs/qunit-1.10.0
    }
 });
 require(['view/eventsView', 
          'test/eventsView_test', 
          'test/eventView_test' ], function(EventsView){
           var events = new EventsView; //here I create first object my View
 });

In eventsView.js initialize I render the main view

  define(['jquery',
          'backbone',
          'underscore',
          'collection/eventC',
          'model/eventM',
          'view/eventView'], function($, Backbone,_,EventC,EventM, EventView,){

 var EventsView = Backbone.View.extend({
    el: $(".contener"),     
    initialize: function(){
        this.render();
     },
     ....//other functions
    });
     return EventsView;
 });

So now I need to call functions from this view in other file eventsView_test.js.
I can’t do it like this because the View will be rendered again:

  define(['jquery','qunit','view/eventsView'], function($,qunit,EventsView){
    //var eventsView = new EventsView(); // I can't create object here 

    test( "first_test_func", function() {
        var result = eventsView.first_test_func(2,2);
        equal( result, 4, "2 square equals 4" );
    });

What should I do? Do I need some kind of singleton or something else?

  • 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-18T12:12:43+00:00Added an answer on June 18, 2026 at 12:12 pm

    Fantastic question, and one that I see all the time.

    I actually solved this by creating what I dubbed a “bootstrap” paradigm, but call it whatever you want. The key is that the Backbone view does not render itself, but instead whomever consumes it is responsible for that. You’ll run into issues with fetch() and the like, so thankfully this solves that too.

    The goal: Don’t unit test render and just skip it all together!

    Your view becomes something like this:

    require(function () {
        var view = Backbone.View.extend({
            // initialize is executed on new, just as you found
            initialize: function (options) {
                _.bindAll(this);
    
                // Create models, initialize properties, and bind to events here.
                // Basically only do __UNEXPENSIVE__, non-dom-changing things that
                // you don't care get executed a lot.                
    
                this.someCollection = new Backbone.Collection();
                this.someCollection.on('add', this.onSomeCollectionAdd);
            },
    
            // bootstrap is for server-side operations that you don't want executed
            // by every single test.
            bootstrap: function () {
                this.someCollection.fetch().done(this.render);
            },
    
            render: function () {
                this.$el.html(...);
            },
    
            first_test_func: function () {
    
            }
        });
    
        return view;
    });
    

    How this would be consumed in your app:

    require(['viewpath'], function (myView) {
        var instance = new myView({ el: $('#something') });
        instance.bootstrap(); //triggers fetch, or if no fetch needed, just render
    });
    

    This can now be unit tested without worry.

    define(['jquery','qunit','view/eventsView'], function($,qunit,EventsView){
    
        test( "first_test_func", function() {
            // Now this line won't call the server, or render.
            // You can isolate/test what you want.
            var eventsView = new EventsView();
            var result = eventsView.first_test_func(2,2);
            equal( result, 4, "2 square equals 4" );
        });
    });
    

    Use SinonJS

    I strongly recommend checking out SinonJS. It’s amazing for unit testing JavaScript, particularly Backbone implementations due to its powerful mocking which can be used from preventing the rendering and server calls (fetch, save, etc) from actually hitting the server while still allowing you to assert they got called.

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I would like to run a str_replace or preg_replace which looks for certain words
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I would like to count the length of a string with PHP. The string
I am using JSon response to parse title,date content and thumbnail images and place
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.