简体   繁体   中英

Testing RequireJS with Jasmine

I'm very new to Javascript and I'm trying to write tests for a project I've joined in on. I have files in the program that look like this:

define([
  'jquery',
  'underscore',
  'backbone',
  'backbone/models/beat',
  'colors',
  'app/dispatch',
  'app/log'
], function($, _, Backbone, BeatModel, COLORS, dispatch, log){
  return Backbone.View.extend({

    getOpacityNumber : function(bool) {
      //code
    },

    unroll: function(){
      //code
    }
  });
});

And I can't figure out how to access these functions in a test. I've tried instantiating an object (although I may be doing it wrong) and calling the functions from there like this:

describe("beatView.js", function() {
    beforeEach( function() {
            var b = new beatView();
    });

    spyOn(console, "log");

    it("test the console log", function() {
        b.unroll();
        expect(console.log).toHaveBeenCalled();
    });
});

But when I run it I get a reference error that Jasmine cannot find the variable b. Is there something that I'm missing? Any help to point me in the right direction would be appreciated.

Try the following:

describe("beatView.js", function() {
    var b = null;
    beforeEach( function() {
            b = new beatView();
    });

    spyOn(console, "log");

    it("test the console log", function() {
        b.unroll();
        expect(console.log).toHaveBeenCalled();
    });
});

I don't know Jasmine wery well, but can't you just describe() the test inside of a require() call?

require(["beatView"], function (beatView) {
  describe(...);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM