|
hace 2 años | |
---|---|---|
.. | ||
README.js | hace 2 años | |
README.md | hace 2 años | |
article.html | hace 2 años | |
folder.jpg | hace 2 años | |
icon.png | hace 2 años | |
jasmine-setup.jpg | hace 2 años | |
run-jasmine.js | hace 2 años |
Contents
\n\nIn this tutorial we will take an existing Ext application and introduce the Jasmine assertion library for unit testing. Readers must be familiar with JavaScript, Ext JS 4, the MVC architecture as well as the fundamentals of HTML, CSS, and using resources.
\n\nWhy Test? There are many reasons to test applications. Tests can verify an application's functionality to eliminate the need to enumerate all the use cases manually. Also, if the application were to be refactored, or updated, the tests could verify that the changes did not introduce new bugs into the system
\n\nFor this tutorial, use the \"simple\" example of the MVC in the ExtJS bundle — found under <ext>/examples/app/simple
. Copy the simple folder to your workspace or desktop.
Add these folders:
\n\n<simple dir>/app-test\n<simple dir>/app-test/specs\n
\n\nDownload and extract the Jasmine standalone library into the app-test folder.
\n\nCreate these files (leave them empty for now, you will fill them in next)
\n\n<simple dir>/app-test.js\n<simple dir>/run-tests.html\n
\n\nNote: These file names are arbitrary. You may name them however you would like. These names were chosen simply from what they are. The app-test.js file is essentially app.js for testing purposes, and run-tests.html is simply the bootstrap.
\n\nYour project should look like this now:
\n\nNow that you have the files and folders set up, we'll create a test-running environment. This will be a web page that, when viewed, will run our tests and report the results. Open the run-tests.html and add the following markup:
\n\n<html>\n<head>\n <title id=\"page-title\">Tester</title>\n\n <link rel=\"stylesheet\" type=\"text/css\" href=\"app-test/lib/jasmine-1.1.0/jasmine.css\">\n\n <script type=\"text/javascript\" src=\"extjs/ext-debug.js\"></script>\n\n <script type=\"text/javascript\" src=\"app-test/lib/jasmine-1.1.0/jasmine.js\"></script>\n <script type=\"text/javascript\" src=\"app-test/lib/jasmine-1.1.0/jasmine-html.js\"></script>\n\n <!-- include specs here -->\n\n <!-- test launcher -->\n <script type=\"text/javascript\" src=\"app-test.js\"></script>\n\n</head>\n<body>\n</body>\n</html>\n
\n\nThere are a few key things to remember here: the Jasmine resources, the Ext JS framework resource and app-test.js. These will need to be included with your tests (this order is important). You will include the specs (Jasmine assertion js files) above the app-test.js and below the rest of the files.
\n\nNext, open app-test.js and copy this code into it:
\n\nExt.require('Ext.app.Application');\n\nvar Application = null;\n\nExt.onReady(function() {\n Application = Ext.create('Ext.app.Application', {\n name: 'AM',\n\n controllers: [\n 'Users'\n ],\n\n launch: function() {\n //include the tests in the test.html head\n jasmine.getEnv().addReporter(new jasmine.TrivialReporter());\n jasmine.getEnv().execute();\n }\n });\n});\n
\n\nThe effect of the above code is a global reference to the Application instance and bootstrap for the Jasmine assertion library. This is accomplished by directly constructing the Application object and storing the reference when the document is ready, bypassing the Ext.application() method.
\n\nNote: this Application definition is not a copy and paste of your regular Application definition in your app.js. This version will only include the controllers, stores, models, etc and when launch is called it will invoke the Jasmine tests.
\n\nNow you should have a working test environment. Open the run-tests.html file in your browser to verify. You should see the Jasmine UI with a passing green bar that reads 0 specs, 0 failures in 0s
. Ex:
Under the specs folder (<simple>/app-test/specs
) create two empty text files named:
example.js\nusers.js\n
\n\nThen go back to the run-tests.html file and add these two lines under the comment <!-- include specs here -->
<!-- include specs here -->\n<script type=\"text/javascript\" src=\"app-test/specs/example.js\"></script>\n<script type=\"text/javascript\" src=\"app-test/specs/users.js\"></script>\n
\n\nNote: Some examples use a *.spec.js
double extention. It is not required, it is nice to indicate what the file is for. (in our case the folder of specs
instead of just the double extension of *.spec.js
)
Start by filling in specs/example.js
. Jasmine's specification syntax is very descriptive. Each suite of tests is contained in a describe function, and each test is defined by an \"it\" function.
Example:
\n\ndescribe(\"Basic Assumptions\", function() {\n\n it(\"has ExtJS4 loaded\", function() {\n expect(Ext).toBeDefined();\n expect(Ext.getVersion()).toBeTruthy();\n expect(Ext.getVersion().major).toEqual(4);\n });\n\n it(\"has loaded AM code\",function(){\n expect(AM).toBeDefined();\n });\n});\n
\n\nTo pass a test (each \"it\" block) simply call expect(someValue).toBe<something>()
Next a more complicated example. Testing a store, which is asynchronous, and retrieved from a Controller. (This is where that global application reference will come in handy) This code goes in specs/users.js
describe(\"Users\", function() {\n var store = null, ctlr = null;\n\n beforeEach(function(){\n if (!ctlr) {\n ctlr = Application.getController('Users');\n }\n\n if (!store) {\n store = ctlr.getStore('Users');\n }\n\n expect(store).toBeTruthy();\n\n waitsFor(\n function(){ return !store.isLoading(); },\n \"load never completed\",\n 4000\n );\n });\n\n it(\"should have users\",function(){\n expect(store.getCount()).toBeGreaterThan(1);\n });\n\n it(\"should open the editor window\", function(){\n var grid = Ext.ComponentQuery.query('userlist')[0];\n\n ctlr.editUser(grid,store.getAt(0));\n\n var edit = Ext.ComponentQuery.query('useredit')[0];\n\n expect(edit).toBeTruthy();\n if(edit)edit.destroy();\n });\n\n});\n
\n\nNotice the \"beforeEach\" function (this will be called before each \"it\"). This function sets up the stage for each test, and this example:
\n\nCombining this with PhantomJS allows us to run these tests from the command line or from a cron job. The provided run-jasmine.js
in the PhantomJS distribution is all that is needed. (you can tweak it to make the output suit your needs, here is an example tweaked version )
Example command line:
\n\nphantomjs run-jasmine.js http://localhost/app/run-tests.html\n
\n\nYou will need to run the tests from a web server because XHR's cannot be made\nfrom the file://
protocol
On Windows and Mac (without mac ports) you will need to download the static binary from here. Once you have it downloaded extract it some place useful. (Ex: <profile dir>/Applications/PhantomeJS
.) Then update your PATH
environment variable to include the phantomjs binary. For Mac's open a terminal and edit either .bashrc
or .profile
and add this line to the bottom:
export PATH=$PATH:~/Applications/PhantomJS/bin\n
\n\nFor Windows, open the System Properties
control panel and select the Advanced
tab and then click on the Environment variables
button. Under the user variables box find and edit the PATH
by adding this to the end:
;%USERPROFILE%\\Applications\\PhantomJS\n
\n\nIf there is not an entry for PATH
in the user variables, add one and set this as the value:
%PATH%;%USERPROFILE%\\Applications\\PhantomJS\n
\n\nSave your work by selecting the OK
buttons and closing the windows.
For Linux users and mac ports users there is a simple command to enter in the terminal to get and install PhantomJS.
\n\nDebian(Ubuntu) based distribution use:
\n\nsudo apt-get install phantomjs.\n
RedHat(Fedora) based distribution use:
\n\nsu -c 'yum install phantomjs'\n
Mac's with mac ports, use:
\n\nsudo port install phantomjs.\n
To verify simply open a terminal (or command prompt) window and type this:
\n\nphantomjs --version\n
\n\nIf there is output (other than \"command not found\") phantomjs is setup and ready for use.
\n\nAbout the Author: Jonathan Grimes (facebook, twitter, google+) is a software engineer at NextThought, a technology start-up company that is currently building an integrated platform for online education.
\n"});