expect - response在AngularJS单元测试(Jasmine)中不起作用

Expect-respond doesn't work in AngularJS unit testing(Jasmine)

本文关键字:Jasmine 不起作用 单元测试 response AngularJS expect      更新时间:2023-09-26

我的目标是通过AngularJS mock服务(jasmine)的预设响应将数据加载到html中。数据。Json也在那里,所以如果模拟响应不起作用,显示的单词将是"http"而不是"mock"。但是响应结果总是"http",这意味着模拟服务并不真正工作。非常感谢你对这个问题的任何建议。

茉莉javascript文件:

describe("DController", function() {
var httpBackend,scope;
beforeEach(module('MyApp'));    
beforeEach(inject(function($rootScope,$httpBackend,$http,$controller) {
    scope = $rootScope.$new();
    httpBackend = $httpBackend;
    DController = $controller('DController', {
            $scope: scope
    });
    httpBackend.expectGET('data.json').respond({
        "name": "mock"
    });
})); })

controller.js文件
.controller('DController',function($scope,$http) {'use strict';
$scope.loadJson = function() { 
    var getDataJson = $http.get('data.json');
    getDataJson.success(function(data, status, headers, config)  {
        $scope.data = data;
    });
}})

和数据。Json就是

{   "name": "http"  }

您是否尝试使用"whenGET"而不是"expectGET"?