简体   繁体   English

如何在模拟Angular服务中从JSON fixture中提取数据

[英]How to pull data from JSON fixture in mock Angular service

I have created the below mock Angular service to test a controller. 我创建了下面的模拟Angular服务来测试控制器。 When I run my tests I get the error: Unexpected request: GET ./fixtures/stats.json . 当我运行我的测试时,我收到错误: Unexpected request: GET ./fixtures/stats.json

mock.players.service.js: mock.players.service.js:

'use strict';

angular.module('mockPlayersService', [])
  .factory('playersService', ['$http', '$q',
    function($http, $q) {
      var playersService = {};

      playersService.stats = $http.get('./fixtures/stats.json');

      playersService.getStats = function() {
        var defer = $q.defer();
        defer.resolve(this.stats);
        return angular.extend({$promise: defer.promise}, this.stats);
      };

      return playersService;
    }]);

Is there something I need to do in my controller spec to tell my tests to expect this GET request, and/or do I need to declare the fixtures path in my karma.config.js files array? 在我的控制器规范中是否需要做一些事情来告诉我的测试期望这个GET请求,和/或我是否需要在我的karma.config.js files数组中声明fixtures路径?

EDIT: Some more info to show my current (working) setup: 编辑:更多信息显示我当前(工作)设置:

playersService.stats = {
  'firstName': 'John',
  'middleName': 'James',
  'lastName': 'Doe',
  'city': 'Cleveland',
  'state': 'OH',
};

playersService.getStats = function() {
  var defer = $q.defer();
  defer.resolve(this.stats);
  return angular.extend({$promise: defer.promise}, this.stats);
};

I simply want to move that current playersService.stats object out into a JSON fixture. 我只想将当前的playersService.stats对象移动到JSON fixture中。

Based on your approach the easiest thing would be to make karma serve your fixture files. 根据您的方法,最简单的方法是让karma为您的夹具文件服务。 In your karma.conf files stanza you can add something like this: 在你的karma.conf文件节中,你可以添加如下内容:

  { pattern:  './fixtures/*.json',
    watched:  true,
    served:   true,
    included: false }

I think karma serves files from a root called base, you may need to play with the url you are giving to $http. 我认为karma提供来自root的根文件,你可能需要使用你给$ http的url。

I don't quite get why you bother with a MockService sistering your actual one. 我不太明白你为什么要打扰MockService姊妹你真正的那个。 It seems like a very heavy weight approach. 这似乎是一种非常重的方法。 The more usual thing to do is to use your actual service and mock the backend. 更常见的事情是使用您的实际服务并模拟后端。 Something like this in your tests: 你的测试中有这样的东西:

before(inject(function( $httpBackend, playersService) {
    o = playersService;
    back = $httpBackend;

  back.whenGET('/therealPath').respond({});


}));

You'd still need a way to get your fixture files loaded, but you could install karma-read-json and then follow a pattern like this: 你仍然需要一种方法来加载你的夹具文件,但你可以安装karma-read-json然后按照这样的模式:

var valid_respond = readJSON('./fixtures/stats.json');
$httpBackend.whenGET('/therealPath').respond(valid_respond);

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

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