A protractor plugin for mocking server responses when not using angular $http service. This use the mockery project to mock out endpoints. Documentation for using mockery are available here.
Should work for:
- JQuery
- XMLHttpRequest
- Custom XHR Objects
Install with npm.
npm install mockery-protractor-plugin --save-dev
Add the following to your protractor.conf.js
file.
plugins: [{
package: 'mockery-protractor-plugin',
files: [
'../../test/mock/state.js',
'../../test/mock/routes.js'
]
}],
The plugin will inject a script into your app at startup that will replace your configured XHR object. You must tell the plugin where your mock files are to inject. The files should contain your state object definitions and all the routes that should be mocked.
For Example:
Mockery.imitate('/login', 'POST', function (req, res) {
res.type('application/json');
if( state.me.email == req.body.username ){
res.statusCode(200);
res.json(state.me);
} else {
res.statusCode(404);
res.json({
"status": "not found"
});
}
});
In the plugin configuration the variable to inject the MockHttpRequest into can be specified with the xhr
property.
plugins: [{
package: 'mockery-protractor-plugin',
xhr: 'XMLHttpRequest'
...
}],