【问题标题】:Expect-respond doesn't work in AngularJS unit testing(Jasmine)Expect-respond 在 AngularJS 单元测试(Jasmine)中不起作用
【发布时间】:2014-07-09 14:35:07
【问题描述】:

我的目标是通过 AngularJS 模拟服务(茉莉花)的预设响应将数据加载到 html。 data.json 也在那里,因此如果模拟响应不起作用,则显示的单词将是“http”而不是“mock”。但是响应结果总是“http”,这意味着模拟服务并没有真正起作用。 非常感谢您对此问题的任何想法。

Jasmine 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;
    });
}})

data.json 就是简单的

{   "name": "http"  }

【问题讨论】:

    标签: javascript angularjs unit-testing getjson


    【解决方案1】:

    您是否尝试过使用“whenGET”而不是“expectGET”?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 2016-05-14
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-21
      相关资源
      最近更新 更多