Question

I'm currently trying to test a JS function with Jasmine. Since the function checks for CSS classes set in the body tag, I want to use the <body> tag in a fixture:

setFixtures('<body class="themed someotherclass"></body>')

The jQuery reference to this stubbed HTML - $('body') - is then handed over to the JS function I'm testing. But this body tag obviously never really arrives inside the Jasmine test - it looks as if Jasmine creates its own document with a body and inside this it puts all the fixtures inside a div - at least when I echo the outerHTML of the stubbed body element I see this inside a body and after a Jasmine-related inline JS:

<div id="jasmine-fixtures"></div>

which is empty when I try to stub the body, but filled when I use a div instead.

Is there a way to stub the body tag within a Jasmine test?

Was it helpful?

Solution

Ok, I finally found the solution.

Whenever you want to stub the body tag with Jasmine, you first have to call "setFixtures()" - during this call Jasmine creates it's own body-tag. So after the body tag has been created, you can manipulate it e.g. using jQuery and then run your test which depends on the changes you have made to the body tag.

That is: I run my usual "setFixtures(SOME HTML)" command if needed or - in case I only need the body tag for my test, I simple call "setFixtures()" in the beginning of my test without any arguments. After that, under Jasmine version 1.x at least, the tag will be available and I can use for example jQuery to adjust/modify the body tag like I need it: $('body').addClass('themed'); So instead of mocking the body tag as a fixture, I "create" the body tag by calling "setFixtures()" without any arguments and then manipulate the DOM as needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top