影响数据库角端到端测试端到、数据库、测试

2023-09-13 02:35:09 作者:?我的人生就TM缺你?

下面是我的问题,一步:) steb)我还以为它读取这样更好,不像文字墙可怜试图解释我非常特定领域的问题。

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)我们有一个 Angular.js 应用与 PHP 后端与支持 MongoDB的存储。

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

2)量角器为终端到终端的测试。

2) Protractor for End-to-end tests.

3)需要测试其修改数据库,即注册页面情景 - 我会通过测试中的所有注册步骤,所以数据库获取一个新用户记录。

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,测试将它运行后失败,因为该数据库已为测试用户的记录,无需注册 - 用户被重定向,而不​​是主页

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.

我想获得的MongoDB 的node.js ,并在测试一个数据库进行交互。结果但它只是似乎不正确:对于DB连接配置文件是在 PHP在后端文件,而我试图写纯粹的前端部分的测试我们的应用程序。

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.

任何想法?

推荐答案

有一个简单的方法来做到这一点。如果你在你的应用的角度服务,与您的后端会谈可以从量角器调用它。

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.

下面是一个例子:

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

这code将被序列化,它会在浏览器上执行。我有一个在ProtractorMeetupApp模块中称为apiService服务。 API服务可以创建,更新等。

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.

您的测试是这样的:

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