我有一个使用AngularAMD / RequireJS / Karma / Jasmine的项目,我的基本配置全部工作,大多数单元测试运行并成功通过.
原文链接:https://www.f2er.com/angularjs/141887.html我无法使用angular.mock.module或angularAMD.value()正确注入模拟服务.
我有:
// service definition in services/MyService.js define(['app'],function(app) { app.factory('myService',[ '$document',function($document) { function add(html) { $document.find('body').append(html); } return { add: add }; }]); } ); // test define(['angularAMD','angular-mocks','app','services/MyService'],function(aamd,mocks,app) { describe('MyService',function() { var myBodyMock = { append: function() {} }; var myDocumentMock = { find: function(sel) { // this never gets called console.log('selector: ' + sel); return myBodyMock; } }; var svc; beforeEach(function() { // try standard way to mock a service through ng-mock mocks.module(function($provide) { $provide.value('$document',myDocumentMock); }); // hedge my bets - try overriding in aamd as well as ng-mock aamd.value('$document',myDocumentMock); }); beforeEach(function() { aamd.inject(['myService',function(myService) { svc = myService; }]); }); it('should work',function() { // use svc expecting it to have injected mock of $document. spyOn(myDocumentMock,'find').andCallThrough(); spyOn(myBodyMock,'append'); svc.add('<p></p>'); expect(myDocumentMock.find).toHaveBeenCalledWith('body'); expect(myBockMock.append).toHaveBeenCalledWith('<p></p>'); }); }); } );
有谁知道我哪里出错了?任何帮助将非常感激.