简体   繁体   English

预期响应在AngularJS单元测试中不起作用(Jasmine)

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

My aim is to load data to the html via pre-set response of the AngularJS mocking service(jasmine). 我的目的是通过AngularJS模拟服务(茉莉花)的预设响应将数据加载到html。 The data.json is also there, so if the mock response doesn't work the words displayed would be "http" instead of "mock". data.json也在那里,因此,如果模拟响应不起作用,则显示的单词将是“ http”,而不是“ mock”。 But the response turns out always be "http" which means the mock service doesn't really work. 但是结果始终是“ http”,这意味着模拟服务实际上并没有起作用。 Very much thankful for any ideas about the issue. 非常感谢您对此问题的任何想法。

Jasmine javascript file: 茉莉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 file 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;
    });
}})

and data.json is simply 和data.json只是

{   "name": "http"  }

您是否尝试使用“ whenGET”代替“ expectGET”?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM