【问题标题】:Jasmine should ignore post data valuesJasmine 应该忽略发布数据值
【发布时间】:2015-12-10 23:33:17
【问题描述】:

我正在尝试使用 Jasmine 测试我的 Angular 应用程序。 我就是这样称呼它的:

$httpBackend
  .expectPOST('http://localhost:3000/booking_requests.json',
    {lat: 58.37, lng: 26.71})
  .respond(201);


sharedProperties.setLatitude(58.37);
sharedProperties.setLongitude(26.71);
scope.submit();
$httpBackend.flush();

这是我在函数中所拥有的:

$scope.submit = function() {

console.log(sharedProperties.getLatitude());
console.log(sharedProperties.getLongitude());
console.log($scope.formdata.name);


var parameter = JSON.stringify({lng: sharedProperties.getLongitude(), lat: sharedProperties.getLatitude(),
  name: $scope.formdata.name});
console.log(parameter);

$http.post('http://localhost:3000/booking_requests.json',parameter )
  .then(function (response) {
    $scope.sync_notification = response.data.eta;
    $scope.modal.show();
    PusherService.subscribe(response.data.id);
  });
};

还有错误:

   TypeError: 'undefined' is not an object (evaluating 'response.data.eta')

所以我必须让它忽略我从 post 方法获得的 json 内容。任何想法或评论将不胜感激。

【问题讨论】:

    标签: angularjs jasmine http-post karma-jasmine


    【解决方案1】:

    作为响应,您返回单个值,而在实际函数中,您从对象中检索值。

    不用传参数,测试用例代码如下:

    $httpBackend.expectPOST('http://localhost:3000/booking_requests.json').respond({data:{eta:201,id:456}});
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-13
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      相关资源
      最近更新 更多