Recently, I stumbled upon a fantastic ng-describe
package that simplifies the process of writing unit tests for AngularJS applications. It eliminates the need to remember and write all the boilerplate code required to load, inject, mock, or spy.
Has anyone experimented with using ng-describe
alongside protractor
? Is it a practical approach and are there benefits to be gained from it?
One feature that stood out to me is how effortlessly you can mock HTTP responses:
ngDescribe({
inject: '$http', // for making test calls
http: {
get: {
'/my/url': 42, // status 200, data 42
'/my/other/url': [202, 42], // status 202, data 42,
'/my/smart/url': function (method, url, data, headers) {
return [500, 'something is wrong'];
} // status 500, data "something is wrong"
},
post: {
// same format as GET
}
},
tests: function (deps) {
it('responds', function (done) {
deps.$http.get('/my/other/url')
.then(function (response) {
// response.status = 202
// response.data = 42
done();
});
http.flush();
});
}
});
Mocking HTTP responses can enhance end-to-end coverage and assess how the UI responds to specific scenarios and handles errors. Currently, we use protractor-http-mock
for this purpose, although there are alternative solutions available which may not be as straightforward as with ng-describe
.