Contact TDD and Jasmine

Source: Internet
Author: User

Recently, I've just learned a little bit about TDD, and here's a brief introduction to how TDD is going,

The full name of TDD is test-driven development (Test-driven development), which is to write the failed test case first, then implement the code from simple to complex under the assurance of the test, and then optimize that refactoring code.

I personally feel that testing and then implementation can make our mind clearer, not write useless code, and in the process of step by step implementation, better help you to think about this problem if the solution, do not have to spend a lot of time to design this algorithm exactly how to write. This can also be used as a software development process, from simple to complex to know development completed.

When I was learning TDD, I used jasmine,jasmine as a test framework for JS, and this article mainly explains the basic use of the Jasmine test framework. First, let's start with one of the simplest test cases:

function () {     It ("should is able to return  function() {         expect (Calculate.count (2,3). Toequal (5);});

First of all, jasmine test function is to start with describe, accept two parameters, one is a string, and one is a function. String is used to describe the test content you want to test (this is to test the Calculate class, here are a lot of methods, I only tested the calculation of two number of the and), function is the code you want to test the function, the IT function is also accept two parameters, one is a string, one is a function, The string also describes the unit content that needs to be tested, there can be multiple it functions, expect is the expectation, and when the expectation is true, the test passes.

Expect provides a variety of methods, such as the toequal here is to determine whether two objects are equal, there are tobe, tobedefined and many other methods. Let's look at a simple test case:

 describe ("example", function   () { var   A; Beforeeach ( function   () {a  = 0< Span style= "color: #000000;"            >;      A  + = 1;      }); Aftereach ( function   () {a  = 0
   
    ;      }); It ( test, 
    function  
     () {expect (a). toequ      Al ( 1
    ); });});
   

Jasmine also provides Beforeeach and Aftereach functions, which, literally, Beforeeach are performed before all the small tests (i.e., it) in the describe (for example, you want to use an instance in a lot of it later, It can be written in Beforeeach), and Aftereach is clearly behind it (it's going to be written in Aftereach after the test is done), which is true. Of course, these two do not necessarily have to be written. Let's look at a simple test case:

Describe ("Guess_number",function(){    varanswer; vargame; Beforeeach (function() {answer=NewAnswergenerator (); Spyon (Answer,' Generate_random_number '). And.returnvalue (' 1234 '); Game=NewGuess (answer);    }); It ("Guess right should is able to return 4a0b!",function() {Expect (Game.guess_answer (' 1234 '). Toequal (true);    }); });

This is the test of a game class that determines whether the number entered is equal to the randomly generated number. Here I used Spyon, when I was studying this, according to the seniors said this can make your function is controllable, such as the generation of random numbers here, the return value is not controllable, you can pass the Spyon to make the return value controllable. (vernacular is assume a variable or function is controllable), corresponding to the spy this test of two functions, tohavebeencalled to test your spy This function is called, The Tohavebeencalledwith is used to test the parameters of the call, and returns true if matched. For example:

Expect (Answer.generate_random_numbe). tohavebeencalled ();

Used to test whether the Generate_random_number function was called.

section below: Jasmine write tests Just remember the first describe (group), it (a single use case), expect (expectation), what matching functions, according to this order can be, in the process of using 1.1 points in depth.

Contact TDD and Jasmine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.