I read this kind of question very often:
How can I test following function with jasmine?
The person who is searching for that answer has searched for examples of jasmine tests and didn't find something which can help him or her.
But there is no good answer to that question. You can make a correct answer by posting a test, which let the function fail or not, but the answer is not good. Why? Because it is the totaly wrong question.
So why should I never ask "How to write a test for that function with jasmine" ?
Why should you never aks: How to test that function with jasmine?
On the jasmines github page the first sentence is:
Jasmine is a Behavior Driven Development testing framework for JavaScript.
So what is Behavior Driven Development? The answer to that topic gives Wikipedia
The principles of BDD are:
So if I am using jasmine, I commit myself to the BDD process. At that means on first hand I am thinking about, what should my program do and how do I want to structure it. That BDD is so different to the function first then unit test process, that you need to think totally different how to programe.
At the beginning I also made the mistake to think first about my class and its methods. But once you know how to structure your programe that it passes the tests, you won't be never want to go back again. Because the result is also totaly different. When your code is easy testable you have a lot of small functions. Every function does just very simple things. That small functions are also very easy to test. You will be surprised how often an expect(myFunction()).toEqual("my result")
is enough to test your code.
So you should never ask: How to test a given function with jasmine? Because it is not the intention of jasmine to test already existing functions. When you write your test first all your functions will be tested.
Something allowed is to ask how to test the behaviour ... with jasmine.