Angular e2e tests affecting database

二次信任 提交于 2019-12-06 06:18:18

问题


Here's my issue, steb by step :)) I thought it reads better this way, unlike a wall of text pitifully trying to explain my very domain-specific problem.

1) We have a Angular.js app with PHP back-end backed with MongoDB storage.

2) Protractor for End-to-end tests.

3) Need to test pages which alter the database, i.e. registration scenario - I'm going through all registration steps in a test, so the db gets a new user record.

4) Predictably, test will fail after it was run, since the db has a record for the test user and no registration is needed - user is redirected to homepage instead.

I was thinking to get a mongodb package for node.js, and interact with a DB in tests.
But it just doesn't seem right: config files for DB connection are in a php files on the backend, while I'm trying to write tests for purely front-end part of our app.

Any ideas?


回答1:


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)
  });
});



回答2:


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



来源:https://stackoverflow.com/questions/20518687/angular-e2e-tests-affecting-database

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