0

I'm using Jasmine for unit testing to test an application with Backbone.js (and it's the first time that I'm working with them both so I'm a little bit stuck :/ ) Here is my Backbone view

define(['jquery','backbone','underscore','handelbars','models/story','text!templates/story.html',
		'controllers/storyController'], 
function($, Backbone, _,handelbars, story,storyTemplate,ctrl){
	var View = Backbone.View.extend({
		el: '#main',
		events:{
			'click .close-story' : 'closeStory',
		}
        // Some functions
	});
	return View;
});

and the spec of Jasmine

define(['views/storyView'],function (storyView) {
  describe("Testing the Story View ",function () {
    var stView;
    beforeEach(function(){
      stView=new storyView({id:1});
      stView.render();
    })
    it("Test if el is defined and trigger the click ",function () {
      expect(stView.el).toBeDefined(); 
    })
  })
})

Thank you :)

6
  • 2
    What exactly is the error you're getting? There is no code matching myView.render().el as mentioned in title. Why are you clicking the button in test before expect(stView.$el.find('.addbtn')).toBeDefined();? Why do you need var self = this; in the shared code? Why are you using $(self.el) instead of self.$el? In short, please cleanup your code, post proper error and if possible create a minimal reproducible example
    – T J
    Commented Aug 7, 2017 at 10:45
  • @TJ Thank you for answering, and I'm sorry I wasn't clear enough, I edited my question and the code I hope it's readable now. PS: I'm not the one who coded the view, I'm working only on tests with jasmine. Thank you so much for your help :) Commented Aug 8, 2017 at 13:42
  • 2
    Still you haven't shared the code that tries to access el property of render method as you mentioned in the error in title: myView.render().el. So the code causing the issue is not present in question, and the content of render function is also not there in question... So we can't figure out what's the problem without seeing the code...
    – T J
    Commented Aug 8, 2017 at 13:51
  • myView refers to stView (I wrote it the title using myView just to make it general) and the code is in the expect of spec published, I hope it's clear now, sorry again :/ Commented Aug 8, 2017 at 13:59
  • 1
    @MariamaMariama Your title states that the issue is with stView.render().el yet your test tests expect(stView.el).toBeDefined(). On the basis of the title, I'd expect the test to be expect(stView.render().el).toBeDefined(). It is still not clear what the problem is exactly, given the inconsistency between the title and code you show. Answers will also most likely depend on the implementation of render(), which as TJ pointed out already, you do not show.
    – Louis
    Commented Aug 8, 2017 at 14:30

1 Answer 1

0

You have hardcode el: '#main' in the view constructor. This will evaluate when the AMD module is loaded. And when you run jasmine unit tests, I don't think this element of your application is available in Jasmine test page, unless you have mocked it somehow. You can test this via putting a break point on the constructor and inspecting the DOM.

For the existing code to work, you should attach a dummy element on whatever DOM jasmine is using to run your tests before loading the module containing view definition.

On the other hand, It's better to remove the hardcoded el: '#main' (You can tell the developer that it's a very bad coding practice) and pass the element reference while creating view instance, so you can do

new storyView({id:1, el : $('<div/>'}); // dummy element for test
4
  • Thank you for responding :) but I need that the element with "main" as id to be loaded so I can compile some handelbars code PS : a div with id="main" existe in a index.html (it's a file in the root of the project) PPS: the index is always loaded before all the views. Thank you ! Commented Aug 15, 2017 at 11:16
  • @MariamaMariama As far as I know Jasmine is for unit testing, and unit tests won't create the DOM structure of your application. So I don't think jasmine will load you're index.html. You have to mock it
    – T J
    Commented Aug 16, 2017 at 9:51
  • I'm sorry but I didn't find how to mock it, all I found is how to mock an object not an html,then I tried to load it using fixture ""jasmine.getFixtures().load("../../index.html");"" in the beforeEach of the spec and using ""define(['text!../../index.html'],function()"" at the head of the spec but no success even if I found the file loaded if I check the network tab while inspecting errors in chrome. Thank you so much :) Commented Aug 17, 2017 at 8:57
  • I don't know how to load fixtures in jasmine either, answers to this question might help stackoverflow.com/q/12715207/2333214
    – T J
    Commented Aug 17, 2017 at 9:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.