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

  • Home
  • SEARCH
  • 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 8766465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:33:35+00:00 2026-06-13T16:33:35+00:00

My test claims to interpolate data into strings, but actually also checks if the

  • 0

My test claims to interpolate data into strings, but actually also checks if the model automatically updates the DOM. How should I handle this situation?

Test for ItemView using Jasmine:

describe("ItemView handles rendering views with a model", function() {

    it("should render a template and interpolate data from a model", function() {

        var user = new Backbone.Model({
            name    : "Peeter",
            age     : 24
        });

        var itemView = new ItemView({
            model       : user,
            template    : 'Hello my name is <%= model.get("name") %> and I\'m <%= model.get("age") %> years old.'
        });

        itemView.render();

        user.set({
            name    : "Pjotr",
            age     : 25
        });

        expect(itemView.$el).toHaveText('Hello my name is Pjotr and I\'m 25 years old.');

    });



});

* The code being tested (BaseView has his own tests) *

My base view:

define(['jquery', 'backbone', 'underscore'], function($, Backbone, _) {
    'use strict';

    var BaseView = Backbone.View.extend({

        /**
         * Update the dom element
         */
        replaceDomElement : function() {

            this.trigger("before:replace_dom_element");

            var $el = this.$el.empty();
            var $clone = $el.clone();
            $el.replaceWith($clone);
            this.setElement($clone);

            this.trigger("after:replace_dom_element");


        },
        /**
         * Default serialize function
         * @returns empty object
         */
        serialize: function(){
            return {};
        },

        /**
         * Render the view, passing the pictures collection to the view and the
         * inner template
         */
        render : function() {

            this.replaceDomElement();

            this.trigger("before:render");

            var template = _.template(this.options.template);
            /**
             * Allow for serialization overwriting
             */
            var options = this.serialize();
            if(this.options.serialize) {
                options = _.isFunction(this.options.serialize) ? this.options.serialize() : this.options.serialize;
            }

            this.$el.append(template(options));
            this.trigger("after:render");

        }

    });

    return BaseView;

});

My item view:

define(['jquery', 'yolojs/views/BaseView'], function($, BaseView) {
    'use strict';

    var ItemView = BaseView.extend({

        className : "item",

        /**
         * Helper function to determine which data should the view pass to the template
         */
        serialize : function() {
            return  {
                model : this.model
            };
        },
        /**
         * Set up our view to autoupdate on change
         */
        initialize: function() {
            /**
             * Assign the cid as a data attribute on the model 
             */
            this.$el.attr("data-cid", this.model.cid);
            this.model.on('change',this.render,this);
        }

    });

    return ItemView;

});
  • 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-13T16:33:37+00:00Added an answer on June 13, 2026 at 4:33 pm

    The simplest way to do so was:

    describe("ItemView", function() {
    
                var user;
                var itemView;
    
                beforeEach(function() {
    
                    user = new Backbone.Model({
                        name    : "Peeter",
                        age     : 24
                    });
    
                    itemView = new ItemView({
                        model       : user,
                        template    : 'Hello my name is <%= model.get("name") %> and I\'m <%= model.get("age") %> years old.'
                    });
    
                });
    
                it("should render a template and interpolate data from a model", function() {
    
                    itemView.render();
    
                    expect(itemView.$el).toHaveText('Hello my name is Peeter and I\'m 24 years old.');
    
                });
    
                it("should call render if the models data changes", function() {
    
                    spyOn(itemView, 'render');
    
                    user.set({
                        name    : "Pjotr",
                        age     : 25
                    });
    
                    expect(itemView.render).toHaveBeenCalled();
    
                });
    
                it("should update the dom element with the new models data", function() {
    
                    user.set({
                        name    : "Pjotr",
                        age     : 25
                    });
    
                    expect(itemView.$el).toHaveText('Hello my name is Pjotr and I\'m 25 years old.');
    
                });
    
    
            });
    

    I also had to change my itemview a bit due to the following: Testing backbone.js application with jasmine – how to test model bindings on a view?

    This answer is credited to @theml

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

Sidebar

Related Questions

I test password recovery, but there are errors. Rspec study recently. code (User Controller)
I say no, partner says yes. We are both stubborn. We will test but
The Intel Instruction set reference claims (emphasis mine): Converts the signed-integer source operand into
I need to convert this into PDO form for a class, but since I
We have to deal with production and test connection strings in our environment. Database
test = function(x){ if ( some conditions ) { return true; } else {
#test code: #!/bin/bash #~/test/test.sh trap echo 'testmessage' DEBUG while : do echo abc sleep
test.c: int main() { return 0; } I haven't used any flags (I am
Test Page URL: http://www.guygar.com/inception/ultra/indexCopy.html I am new to this and searching SO I found
cat test.txt #this is comment line 1 line 2 #this is comment at line

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.