Unit testing becomes even more important when you program in a scripting language like Javascript, since you do not have the compiler rescueing you at least from syntactic mistakes.
After playing a little bit with Ruby I could not resist from falling in love with RSpec and its incredible expressivity! So the day I came across Jasmine I had no doubt about what to use to test my JS!
As it says in the introduction the design of Jasmine has been inspired by RSpec but also by other valid alternatives like JSpec.
Jasmine comes in different flavors: stand-alone, Ruby Gem, Node.js module and even Maven plugin! We are going to use the stand-alone version for now. You can download it from http://pivotal.github.com/jasmine/download.html
Downloaded one of the zip, and unpacked in some folder you will find a file called SpecRunner.html. This is the file containing everything you need to setup Jasmine and get started with testing.
Let's have a closer look:
- <link rel="stylesheet" type="text/css" href="lib/jasmine-1.0.0.rc1/jasmine.css">
- <script type="text/javascript" src="lib/jasmine-1.0.0.rc1/jasmine.js"></script>
- <script type="text/javascript" src="lib/jasmine-1.0.0.rc1/jasmine-html.js"></script>
Then we find the files containing the code we want to test:
- <script type="text/javascript" src="src/Player.js"></script>
- <script type="text/javascript" src="src/Song.js"></script>
- <script type="text/javascript" src="spec/SpecHelper.js"></script>
- <script type="text/javascript" src="spec/PlayerSpec.js"></script>
- <script type="text/javascript">
- jasmine.getEnv().addReporter(new jasmine.TrivialReporter());
- jasmine.getEnv().execute();
- </script>
-
describe(): groups tests together providing also setup and teardown hooks-
it(): contains the test itselfBoth of them take as first parameter a string, used as a description of what is going to be tested (for
describe() ) and what is the expected outcome (for it()
). This is something very familiar to the RSpec's users and it makes it
much easier to analyze the results of the tests once they have run.Other important features are expectations, equivalent to asserts in JUnit.
So let's write a simple unit test for a function validating an email address. According to Test Driven Development let's write the tests first.
Edit a file under the spec folder called
demo_spec.js, with:- describe("Email validation", function() {
- it("should validate someone@somewhere.com",function(){
- console.log("aaa");
- var result = is_valid("someone@somewhere.com");
- expect(result).toBe(true);
- });
- it("should not validate someone@somewhere",function(){
- var result = is_valid("someone@somewhere");
- expect(result).not.toBe(true);
- });
- });
toBe() with the relative not.toBe(), which is the logical not.Let's now write the is_valid function in
demo.js, which will be placed under src:- is_valid = function(email){
- var reg_expr = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
- return reg_expr.test(email);
- };
- <title>Jasmine Test Runner</title>
- <link rel="stylesheet" type="text/css" href="lib/jasmine-1.0.0.rc1/jasmine.css">
- <script type="text/javascript" src="lib/jasmine-1.0.0.rc1/jasmine.js"></script>
- <script type="text/javascript" src="lib/jasmine-1.0.0.rc1/jasmine-html.js"></script>
- <script type="text/javascript" src="src/demo.js"></script>
- <script type="text/javascript" src="spec/demo_spec.js"></script>

Just for curiosity let's change the second test to:
- it("should not validate someone@somewhere",function(){
- var result = is_valid("someone@somewhere");
- expect(result).toBe(true);
- });
And in fact it does!

No comments:
Post a Comment