Jasmine Writing JavaScript Test Cases

Source: Internet
Author: User

1, download Jasmine package, mainly includes the following three files:

1>,jasmine-html.js,jasmine.css,jasmine.js

2>, write the test case with the following code:

Describe ("Examples of Jasmine Suite", function () {//list 1 describe ("This was an exmaple suite", function () {            It ("contains spec with an expectation", function () {expect (true). ToBe (True);            Expect (false). ToBe (false);        Expect (false). Not.tobe (True);    });    });        List 2 Describe ("Test suite is a function.", function () {var gVar;            It ("Spec is a function.", function () {GVar = true;        Expect (GVar). ToBe (True);        });            It ("Another spec is a function.", function () {GVar = false;        Expect (GVar). ToBe (false);    });    });            List 3 Describe ("This is a exmaple suite", function () {It ("contains spec with an expectation", function () {            var num = 10;        Expect (num). toequal (10);    });    });  List 4 Describe ("The ' toBe ' matcher compares with = = =", function () {It ("and have a positive case", function () {expect (true). TOBe (TRUE);        });        It ("and can has a negative case", function () {expect (false). Not.tobe (True);    });    }); List 5 Describe ("Included matchers:", function () {It ("the ' ToBe ' Matcher", function () {var a = 3.            6;            var B = A;            Expect (a). ToBe (b);        Expect (a). Not.tobe (NULL);        });                Describe ("The ' toequal ' Matcher", function () {It ("works for simple literals and variables", function () {                var a = "VarA";            Expect (a). Toequal ("VarA");            });                It ("Work for Objects", function () {var obj = {a:1, b:4                };                var obj2 = {a:1, b:4};            Expect (obj). toequal (OBJ2);        });        });           It ("The ' tobedefined ' Matcher", function () {var obj = {defined: ' Defined ' };            Expect (obj.defined). tobedefined ();        Expect (obj.undefined). not.tobedefined ();    });    });        List 6 Describe ("An example of Setup and Teardown", function () {var gVar;            Beforeeach (function () {GVar = 3.6;        GVar + = 1;        });        Aftereach (function () {GVar = 0;        });        It ("After Setup, GVar has new value.", function () {expect (GVar). Toequal (4.6);        });            It ("A spec contains 2 expectations.", function () {GVar = 0;            Expect (GVar). toequal (0);        Expect (true). Toequal (True);    });    });        List 7 describe ("A spec", function () {var gVar;            Beforeeach (function () {GVar = 3.6;        GVar + = 1;        });        Aftereach (function () {GVar = 0;        });        It ("After Setup, GVar has new value.", function () {expect (GVar). Toequal (4.6);        }); It ("A spec contains 2 expectationS. ", function () {GVar = 0;            Expect (GVar). toequal (0);        Expect (true). Toequal (True);        });            Describe ("nested describe", function () {var tempVar;            Beforeeach (function () {TempVar = 4.6;            });            It ("GVar is global scope, TempVar are this describe scope.", function () {expect (GVar). Toequal (TempVar);        });    });    });        List 8 Xdescribe ("An example of xdescribe.", function () {var gVar;            Beforeeach (function () {GVar = 3.6;        GVar + = 1;        });        XIT ("and xit", function () {expect (GVar). Toequal (4.6);    }); });});

  

3>, write a list.html file as a portal file that calls Jasmine and test cases, with the following code:

4>, open the list.html file in the browser, you can use the view Lists.js file to write the test case of all the results of the execution report, if the test case execution error, you will be prompted on the interface which test case execution errors, such as:

Demo download Download Password: a363

Jasmine Writing JavaScript Test Cases

Related Article

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.