To start with jasmine, let me tell you about jasmine and what it do.
What is Jasmine?
That was brief introduction of jasmine which you can also get on their website. Writing test in jasmine is not very difficult. Every test suite start with a function called ‘describe’ which takes two input.
- A string which is description of test case.
- A function is method or block of code which implements suite.
Second block of test is ‘it’ block. ‘it’ take same inputs as ‘describe’, a string and a function. A simple jasmine test is shown below.
Here arguments of ‘expect’ will be output of function to be tested, and ‘toEqual’ will have expected result as argument.
Download latest standalone release from standalone download page of Jasmine. This will look like this
Open ‘SpecRunner’ in browser and you will look something like this.
Now add following function in ‘src/Player.js’ file.
Now we will add tests for this function. Add following code in spec/PlayerSpec.js.
You will see result of your new tests like this when you will open ‘SpecRunner.html’ in browser.
You can see that we have to repeat same ‘it’ block again and again. The only thing which is changing is description of test, input of function and expected output of function. Here we can use scenarios to avoid repetition of code.
Scenarios In Jasmine tests
Write your scenarios like this
You can see that we just make a separate object for every test case and write all required data for a test in it. Now change your test like this
Now when you will run your tests, you will see same output as before. This is how we can use scenarios to avoid repetition of code in our tests.