Skip to content

Testing

temi edited this page Jul 5, 2021 · 2 revisions

Grails

The grails/groovy code is tested using the Spock framework. Tests live in the src/tests/groovy/ and src/integration-test/groovy directories.

Angular JS

The UI layer of the profile hub is written in AngularJS. We use the Jasmine test framework and the Karma test runner to test the Angular code. This guide tells how to install and run the tests on Mac OS (tested with Yosemite).

Install Karma

  1. Navigate to the root directory of the profile-hub project
  2. Execute the following:
    1. brew install node.js
    2. npm install karma
    3. npm install karma-jasmine karma-chrome-launcher --save-dev
    4. npm install -g karma-cli
    5. npm install karma-coverage
    6. npm install karma-ng-html2js-preprocessor --save-dev
  3. Install other optional browser launchers:
    1. npm install karma-firefox-launcher
    2. npm install karma-safari-launcher
    3. npm install karma-ie-launcher
    4. Update the browsers: ['Chrome'] property in karma.conf.js to add additional browsers to test against.
  4. The source repository contains a karma.conf.js file: this is configures the Karma test runner
  5. (Optional) Install the IntelliJ Karma test runner:
  6. Preferences -> Plugins -> Search for Karma -> Install (at the time of writing there was only 1 matching plugin)
  7. Create a run configuration:
    1. Run -> Edit Configurations -> New -> Karma
    2. Give it a name
    3. Select the karma.conf.js file from the root of the project directory as the Configuration File
    4. The other default values are fine.

Writing Tests

The karma.conf.js file contains a list of all Javascript resources required for testing in the files list. If you create a new file, you must update the karma.conf.js file or your tests won't work.

// list of files / patterns to load in the browser
files: [
    'grails-app/assets/thirdparty/angular-1.3.13.min.js',
    'grails-app/assets/thirdparty/angular-mocks-1.3.13.js',
    'grails-app/assets/thirdparty/ui-bootstrap-tpls-0.12.0.js',
    'grails-app/assets/javascripts/profileEditor/profiles.js',
    'grails-app/assets/javascripts/profileEditor/utils/*.js',
    'grails-app/assets/javascripts/profileEditor/services/*.js',
    'grails-app/assets/javascripts/profileEditor/controllers/*.js',
    'grails-app/assets/javascripts/profileEditor/templates/*.tpl.htm',
    'grails-app/assets/javascripts/profileEditor/directives/*.js',
    'src/test/js/specs/MockConfigModule.js',
    'src/test/js/specs/**/*.js'],

The order is important!

Jasmine tests live in the src/tests/js/specs/ directory. Update existing files or add new ones as necessary.

Code Coverage

You can see a code coverage report for the javascript files by following these steps:

  1. Command Line
    1. Run karma start
    2. Open coverage/<browser>/index.html
  2. IntelliJ
    1. Run the karma configuration using the Run 'karma' with Coverage option (the 3rd toolbar button after the run dropdown).
    2. Open coverage/<browser>/index.html
    3. IntelliJ will also annotate the filenames in the project explorer with a percentage of lines covered.
Clone this wiki locally