Use Jasmine for Function Testing in sencha touch Development

Source: Internet
Author: User

For Jasmine, I will not introduce it more. Can go to the official website to see http://pivotal.github.com/jasmine/

Although sencha touch (hereinafter referred to as St) has some bugs, it does not affect the use of the project, because we only use the features and UI parts provided by sencha touch. In my personal opinion, Mixin is better. Previously, openlaszlo also supported Mixin, so it is almost the same. In the development process, unit testing is currently not easy to do, It should be JavaScript. Although there are some third-party testing frameworks, such as jsunit, Jasmine, and dojo doh, they still cannot meet their own needs, or they are complicated logic or processes, writing unit tests can be difficult. For many employees who are new to Javascript, it is even more difficult.

I chose Jasmine because the ST group is also in use. I also read some of its unit tests, including carefully studying Jasmine, and found that this is a good thing. Introduce the project.

Before writing this simple example, I have to give a basic introduction to the Mixin of extjs. We know that there are interfaces in advanced languages, such as Java. To enhance the functions of some classes, or many classes may have some common functions, they can only be implemented through abstract interfaces. Javascript has no interface concept, so extjs introduces Mixin, but it is not actually a real interface. Is to enhance a class of behavior. Many may say that extjs class system does not have inheritance. But we need to know that single inheritance is far from enough for a class. Let's take their component source code as an example:

Ext.define('Ext.Component', {    extend: 'Ext.AbstractComponent',    alternateClassName: 'Ext.lib.Component',    mixins: ['Ext.mixin.Traversable'],    ...});

As you can see above, the class inherited by component is Ext. abstractcomponent, and it also has the method interface provided by the Ext. Mixin. traversable class. If only inheritance is used, it is difficult to achieve this.

In addition, extjs introduces puremvc and uses some common design patterns, such as controller and Mixin, to achieve this purpose,

Ext.define('Ext.app.Controller', {    alternateClassName: 'Ext.Application',        mixins: {        observable: 'Ext.util.Observable'    },  ...});

For more details, you can refer to some of the source code.

Back to our topic, I will show you how to perform basic tests. First, define your own class, which is a singleton,

Ext.define('Ext.util.HashUtil', {    statics: {        getInstance: function() {            if (!this.instance) {                this.instance = new this();            }            return this.instance;        },        setInstance: function(instance) {            this.instance = instance;            return this;        }    },     ...});

The jasmine code is as follows: first define our own matchers (https://github.com/pivotal/jasmine/wiki/Matchers ):

beforeEach(function() {  this.addMatchers({    isSingleton: function(instance){        var currentInstance = this.actual;        return instance === currentInstance;    }  });});

Step 2: Write down the test group of the current class

describe("HashUtil", function() {    var hashUtil;        beforeEach(function() {        hashUtil = Ext.util.HashUtil.getInstance();    });    it("HashUtil should be a singleton", function() {        var hashUtil0 = Ext.util.HashUtil.getInstance();        expect(hashUtil).toBe(hashUtil0);    });...});

Then you can perform the test. Test result diagram:

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.