1
votes

I have code that is embedded in html as follows:

<script type="text/javascript"> 
     function add(a, b) {
         return a+b; 
     }
</script>

And I was wondering if anyone knew if this was possible to test using Jasmine and the JQuery fixtures? I am new to Jasmine and don't understand how to test this kind of code.

Thanks.

1
why does it have to be embedded and cannot be added as a separate file?pawlik
I cannot change the file that I'm testing.Annika Peterson

1 Answers

1
votes

I think @pawlik is suggesting it is better belonging in it's own .js file. The only option I considered to answer your question is to source this file as a fixture (using the loadFixtures('file') method) but you normally do this with fragments (nothing that contains html, head or body tags) and if there are any on DOM ready events which are fired in script tags or includes they may not place nicely with jasmine.

Saying you can't change the file may not mean you cannot change the structure (ie. push it out to a separate script file and refer to that in the main HTML page.) In many cases you should refactor the structure of your code to facilitate testing.

Since you are new to jasmine, when writing tests you tend to test javascript functions/methods on their own, or with fragments of HTML (if you are doing DOM-based testing) as fixtures. You wouldn't normally use a complete HTML page and write tests for it, this is more of an end-to-end / functional test and is not the primary goal of jasmine.

BTW you are missing the end double-quote of "text/javascript <--