javascript - How to pull data from JSON fixture in mock Angular service -


i have created below mock angular service test controller. when run tests error: unexpected request: ./fixtures/stats.json.

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 need in controller spec tell tests expect get request, and/or need declare fixtures path in karma.config.js files array?

edit: more info show 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 want move current playersservice.stats object out json fixture.

based on approach easiest thing make karma serve fixture files. in karma.conf files stanza can add this:

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

i think karma serves files root called base, may need play url giving $http.

i don't quite why bother mockservice sistering actual one. seems heavy weight approach. more usual thing use actual service , mock backend. in tests:

before(inject(function( $httpbackend, playersservice) {     o = playersservice;     = $httpbackend;    back.whenget('/therealpath').respond({});   })); 

you'd still need way fixture files loaded, install karma-read-json , follow pattern this:

var valid_respond = readjson('./fixtures/stats.json'); $httpbackend.whenget('/therealpath').respond(valid_respond); 

Comments