2015년 8월 25일 화요일

Write javascript test case with Jasmine

Jasmine is one of the javascript test-framework. As described in official introduction documents, It is a  "Behavior-Driven JavaScript Test Framework" and it provides many useful API to write test case easily. If you have a experience about junit in java world, you can easily learn it.

Whenever I learn new programming language, I have been trying to learn its unit-test framework first. It is a quite useful. By writing learning test case from the very beginning of learning, you can easily learn how to test it with basic knowledge of your programming language. In most of cases, the code that easy to test is more easy to maintain. Well-made test cases can be a specification of user's requirements. It is more useful than well-made document. As application is going forward, it can be guard to protect legacy functionality.  


1.Write test case

Jasmine test have a following structure. You can define test suite with "describe" method. It can have many test cases. Each of test case is defined by "it" method. The first argument of "it" method like [line 2], It is a description of test case. The second argument is a function which is called when this test case is executed. The expected result of logic or expected return value of function can be defined in "expect" method. With "toBe" method, you can call actual logic or function which you want to test. Not only "toBe", but Jasmine also provides many useful Matcher and API. You can find details in API documents


2. Execute test case

Then, How can we execute test case? You need to download Jasmine from release page. There is a zip file. In there, you can find "SpecRunner.html". As see in [line 15], it includes source files which contains some logic. [line 18] includes test case file which contains test case to test logic or function in source files.

If open "SpecRunner.html" with web browser, you can see the followings,


























3. Tools

Debugging and checking console logs are very routine work when we do programming. Chrome and Firefox provides very convenient tools for this. Below is developer tool of Chrome browser.











My mother language is not english. Please understand my poor english. :D 

댓글 없음:

댓글 쓰기