0
我試圖測試一個Angular服務。我有一個常數叫做keys
。我試圖嘲笑這個常數,但我仍然得到真正的常數。下面是我的測試:我的sinon存根未顯示在我的Angular服務中
describe('getSuperheroes()', function() {
var mockKeys;
beforeEach(function() {
mockKeys = sinon.stub({apikey: 'test'});
module(function($provide) {
$provide.value('keys', mockKeys);
});
});
it('should populate superheroes[] with result from get()', inject(function(superheroService, $httpBackend) {
expect(superheroService.superheroes).toBeNull();
$httpBackend.when('GET', 'http://gateway.marvel.com/v1/public/characters?apikey=test').respond({
data: {
results: 100
}
});
superheroService.getSuperheroes();
$httpBackend.flush();
expect(superheroService.superheroes).toBe(100);
}));
這裏是我的keys
模塊:
angular.module('marvelApp').constant('keys', {
apikey: 'vip number'
});
這裏是我的服務:
angular.module('marvelApp').factory('superheroService', function($resource, $q, keys) {
return {
superheroes: null,
getSuperheroes: function() {
var deferred = $q.defer();
var that = this;
if(this.superheroes) {
deferred.resolve(that.superheroes);
} else {
this.getResource().get({
apikey: keys.apikey
}, function(data) {
that.superheroes = data.data.results;
deferred.resolve(data.data.results);
});
}
return deferred.promise;
},
...
};
});
什麼我在我的測試做錯了,是不是讓我嘲笑keys
模塊。謝謝您的幫助。
請記住選擇您自己的答案:) –