Jasmine Mox Matchers
This package include some matchers for testing Angular promises without having to $digest
manually and two other useful matchers.
They were made for the Resource testing DSL in Mox, but also very useful when included separately.
- Promise matchers
- Matcher for testing query parameters
- Matcher for directive isolate scope testing
Installing
bower install jasmine-mox-matchers --save-dev
or npm install jasmine-mox-matchers
.
Include src/jasmine-mox-matchers.js
or dist/jasmine-mox-matchers.min.js
file in the files list of your test runner config files.
Or when your are using ES6 modules: `import { jasmineMoxMatchers } from 'jasmine-mox-matchers';
;
Documentation
Promise matchers
Promises are a powerful concept in Javascript but somewhat hard to test. Your test case usually may look like this:
SomeService // Returns a promise that resolves to 'data' ;
With the promise matchers, this is all you need to do:
;
Apart from the 3 lines of boilerplate code, the usual way of testing does not guarantee that your promise will resolve!
If the promise does not resolve or rejects, the expect
will not be called and the test passes because the expect
statement is not called.
Note that a lot of promise matchers on Github still work this way!
In short, these promise matchers are really clean to use and have a correctly implemented 'failing' case.
Query parameter matcher
Test if query parameters exist in a certain URL.
;
Strict matching is also supported by passing true
as second argument.
var path = 'path?param1=param1¶m2=param2';; // This fails; // This passes
Directive matcher
When you mock a directive away, you still want to test of scope vars are passed to the directive correctly. This can be tested by testing the directive attribute. These attributes usually are models or expressions, so you will be testing the literal value of the attribute. It is better to test the evaluated value, which can be done by testing the isolate scope of the directive.
$scopelist = 'first' 'second';$scopetitle = 'The title';var element = $scope;$scope;;
API
toBePromise()
Tests if a given object is a promise object. The Promises/A spec (http://wiki.commonjs.org/wiki/Promises/A) only says it must have a function 'then', so, I guess we'll go with that for now.
;
toResolve() / toHaveBeenResolved()
Asserts that a Promise is resolved.
;
toResolveWith() / toHaveBeenResolved()
Verifies that a Promise is resolved with the specified argument.
;
If you pass a function, you can use that as callback to get the resolved value and some some further assertions on it.
;
toReject() / toHaveBeenRejected()
Asserts that a Promise is rejected before the end of the test.
;
toRejectWith() / toHaveBeenRejectedWith()
Asserts that a Promise is rejected with the specified argument.
;
If you pass a function, you can use that as callback to get the resolved value and some some further assertions on it.
;
toContainIsolateScope()
Asserts that the passed key/value pairs are on the isolate scope of the element.
;
Development
npm install
Run npm run
to see available tasks.