Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何断言后端端点已被调用?_Javascript_Angularjs_Unit Testing_Protractor_Angularjs E2e - Fatal编程技术网

Javascript 如何断言后端端点已被调用?

Javascript 如何断言后端端点已被调用?,javascript,angularjs,unit-testing,protractor,angularjs-e2e,Javascript,Angularjs,Unit Testing,Protractor,Angularjs E2e,我正在使用量角器对角度应用程序进行E2E测试。正在使用$httpBackend模拟后端HTTP服务。到目前为止,测试如下所示: describe('foo', function () { it('bar', function () { var backendMockModule = function () { angular .module('backendMock', []) .run(['$httpBackend', function

我正在使用量角器对角度应用程序进行E2E测试。正在使用
$httpBackend
模拟后端HTTP服务。到目前为止,测试如下所示:

describe('foo', function () {
  it('bar', function () {
    var backendMockModule = function () {
      angular
        .module('backendMock', [])
        .run(['$httpBackend', function ($httpBackend) {
          $httpBackend.whenPUT('http://localhost:8080/services/foo/bar')
            .respond(function (method, url, data, header) {
              return [200, {}, {}];
            });
        }]);
    };
    browser.addMockModule('backendMock', backendMockModule);

    browser.get('http://localhost:8001/#/foo/bar');

    element(by.id('baz')).click();

    // here I would like to assert that the Angular app issued a PUT to '/foo/bar' with data = {...}
  });
});
这个测试比这个要详细一点,它测试接口和其他东西的乐观更新。但我认为这和这个问题无关,所以我删除了其他部分。测试本身运行良好,我能够检查接口上的元素是否符合预期。我没有发现的是:

如何断言已使用正确的数据、方法、头等调用后端HTTP端点?

我尝试过这样做(添加
hasBeenCalled
变量):

但它不起作用。我不知道量角器是如何进行测试的,但我想它会在call
addMockModule
中将函数的序列化版本发送到浏览器,而不是在与网页相同的过程中运行测试,因此我无法在测试和浏览器之间共享状态(附带问题:是否正确?)

describe('foo', function () {
  it('bar', function () {
    var hasBeenCalled = false;

    var backendMockModule = function () {
      angular
        .module('backendMock', [])
        .run(['$httpBackend', function ($httpBackend) {
          $httpBackend.whenPUT('http://localhost:8080/services/foo/bar')
            .respond(function (method, url, data, header) {
              hasBeenCalled = true;
              return [200, {}, {}];
            });
        }]);
    };
    browser.addMockModule('backendMock', backendMockModule);

    browser.get('http://localhost:8001/#/foo/bar');

    element(by.id('baz')).click();

    expect(hasBeenCalled).toBeTruthy();
  });
});