我是AngularJS的新手,当然还有JS测试。我陷入了如何测试角度控制器内部驱动的数据的问题。我已经了解到,sinon可以通过函数调用(如服务)来获取伪数据。我也知道Angular有$提供......但是因为我是新人,我迷路了。所以我想问你的选择,我做错了什么,接下来我应该怎么做才能为我的Angular应用程序正确编写JS测试。
控制器
(function () {
'use strict';
var controllerId = 'appCtrl';
angular.module('app').controller(controllerId,
['common', 'datacontext', appCtrlFn]);
function appCtrlFn(common, datacontext) {
var vm = this;
vm.title = 'My Title';
vm.allFooData = [];
vm.count = 0;
activate();
function activate() {
common.activateController([getAllFooData()], controllerId)
.then(function () { log('Activated.'); });
}
function getAllFooData(forceRefresh) {
return datacontext.allFooData.getAll(...)
.then(function (data) {
vm.allFooData = data;
getCount();
return data;
}
);
}
function getCount() {
return datacontext.allFooData.getCount()
.then(function (num) {
return vm.count = num;
});
}
}
})();
datacontext.allFooData实际上是一个角度服务(对于Foo)
(function () {
'use strict';
var serviceId = 'repository.foo';
angular.module('app').factory(serviceId,
[fooRepositoryFn]);
function fooRepositoryFn() {
function ctor() {
this.getAllFooData = getAllFooData;
this.getCount = getCount;
}
return ctor;
function getAllFooData() {
return [
{
"id": 1,
"name": "foo1",
},
{
"id": 2,
"name": "foo2",
},
{
"id": 3,
"name": "foo3",
},
];
}
function getCount() {
return 3;
}
}
})();
我的测试代码
describe("Controller Tests", function () {
var controller,
controllerName = 'appCtrl',
service,
serviceName = 'repository.foo';
beforeEach(function() {
// Load app module
module('app');
inject(function($injector) {
// Get the controller
controller = $injector.get('$controller')(controllerName);
// Get the service
service = $injector.get(serviceName);
});
});
describe("Test case 1", function() {
it("Should be created", function() {
expect(controller).toBeDefined(); // PASSED
});
it("Should have title", function() {
expect(controller.title).toEqual('My Title'); // PASSED
});
});
describe("Repository Test", function() {
it("Should be defined", function() {
expect(service).toBeDefined(); // PASSED
});
it("Should get data", function () {
// Stub the 'getCount' function from the service (is it internal)?
sinon.stub(service, 'getCount', function () { // ERROR
// I don't want to touch the real data, so I created a fake
var count = fooDataMock.getAllFooData().length; // returns 1
return count;
});
expect(service.getCount).toEqual(1); // FAILED
});
});
})
这是错误消息
TypeError: Attempted to wrap undefined property getCount as function
at Object.wrapMethod (http://localhost:9876/base/Scripts/sinon-1.11.0.js:739:25)
at Object.stub (http://localhost:9876/base/Scripts/sinon-1.11.0.js:2539:26)
at null.<anonymous> (http://localhost:9876/base/app/tests/foo/foo.spec.js:70:19)
at jasmine.Block.execute (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:1145:17)
at jasmine.Queue.next_ (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2177:31)
at jasmine.Queue.start (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2130:8)
at jasmine.Spec.execute (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2458:14)
at jasmine.Queue.next_ (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2177:31)
at jasmine.Queue.start (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2130:8)
at jasmine.Suite.execute (http://localhost:9876/absolute.../npm/node_modules/karma-jasmine/lib/jasmine.js:2604:14)
答案 0 :(得分:0)
使用ctor
对象和函数调用:
expect(service.ctor.getCount()).toEqual(1)