Angular e2e tests affecting database

允我心安 提交于 2019-12-04 11:05:21

There is an easy way to do it. If you have an angular service in your app that talks with your back-end you can call it from protractor.

Here is an example:

https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js

function createObject(data) {
  return browser.executeAsyncScript(function(data, callback) {
    var api = angular.injector(['ProtractorMeetupApp']).get('apiService');
    api.member.save(data, function(newItem) {
      callback(newItem._id);
    });
  }, data);
}

This code will be serialized and it will be executed on the browser. I have a service called apiService in the ProtractorMeetupApp module. The api service can create, update, etc.

Your test would look like this:

https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/member3-spec.js

it('should call api', function() {
  // Create a new member.
  createObject({name: 'test member'}).then(function(id) {
    console.log(id)
  });
});
pherris

I would mock out the interactions with your PHP app. This will allow you to isolate your tests to the Angular code and test for edge cases in your data (or issues on the server side) more explicitly. http://docs.angularjs.org/api/ngMockE2E.$httpBackend

Here is another answer that might be helpful for you: mock $httpBackend in angular e2e tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!