Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.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
测试返回承诺的AngularJS服务时遇到问题_Angularjs - Fatal编程技术网

测试返回承诺的AngularJS服务时遇到问题

测试返回承诺的AngularJS服务时遇到问题,angularjs,Angularjs,我不能让这个测试工作。服务mqttService返回 return $q(function(resolve, reject) { var client; function onConnected() { var message = new Paho.MQTT.Message(payload); message.destinationName = sendConfig.destination; client.subscribe(sendCon

我不能让这个测试工作。服务mqttService返回

return $q(function(resolve, reject) {

    var client;

    function onConnected() {
      var message = new Paho.MQTT.Message(payload);
      message.destinationName = sendConfig.destination;
      client.subscribe(sendConfig.subscribe);
      client.send(message);
    }

    function onMessageArrived(message) {
      resolve(message.payloadString);
    }

    function onFailure() {
      reject({
        message: "can't connect"
      });
    }

    function onConnectionLost() {
      reject({
        message: "connection lost"
      });
    }

    client = new Paho.MQTT.Client(config.getMqttConfig().host,
      config.getMqttConfig().port,
      sendConfig.clientId);

    client.onConnectionLost = onConnectionLost;
    client.onMessageArrived = onMessageArrived;

    client.connect({
      onSuccess: onConnected,
      onFailure: onFailure
    });


  });
此服务的目的是连接到MQTT服务器并从订阅返回值

我已验证是否调用了client.connect,但在服务器日志中甚至看不到连接尝试

我知道我可以模拟事物,但我不想进行集成测试

这就是测试现在的样子:

describe('Mqtt Service Unit Tests', function() {
  var mqttService, rootscope;
  beforeEach(module('starter.factories'));

  beforeEach(inject(function(_mqttService_, $rootScope) {
    mqttService = _mqttService_;
    rootScope =  $rootScope;
  }));

  it('can call service', inject(function(mqttService) {

    expect(mqttService).toBeDefined();

    var result = mqttService.send("{'id': hello}", {
      subscribe: 'mydata/test',
      destination: 'mydata/test',
      clientId: '12345'
    });

    rootScope.$digest();

    result.then(function(response) {
      console.log(response);
    }).catch(function(error){
      console.log(error);
    });

   expect(1).toEqual(1);

  }));

});

问题是当前的1.9.x PhantomJS版本不支持WebSocket。因此,角度没有问题。是的,我现在正在这样做