如何在angularJS中编写工厂方法的单元测试
我有一个工厂方法,如下所示:如何在angularJS中编写工厂方法的单元测试,angularjs,unit-testing,karma-jasmine,factory,Angularjs,Unit Testing,Karma Jasmine,Factory,我有一个工厂方法,如下所示: createApp: function(appType, subjectType, appIndicator) { if (appType === 'newApp') { return Restangular.all('v1/app').post({appType: appType, appIndicator: appIndicator}).then(function(app) { $state
createApp: function(appType, subjectType, appIndicator) {
if (appType === 'newApp') {
return Restangular.all('v1/app').post({appType: appType, appIndicator: appIndicator}).then(function(app) {
$state.go('app.initiate.new', {appId: app.id});
});
} else {
return Restangular.all('v1/app').post({appType: appType, subjectType: subjectType, appIndicator: appIndicator}).then(function(app) {
$state.go('app.initiate.old', {appId: app.id});
});
}
}
我想为它编写单元测试…但我不确定从哪里开始测试它。我只为工厂方法编写了比这简单得多的单元测试(比如简单的数学函数)
我正在使用karma+jasmine进行测试,到目前为止,我写了一些类似的东西,但失败了
it('should return a new application', function(done) {
application.createApp().then(function(app) {
expect(app.appType).toEqual("newApp");
done();
});
});
关于如何进行这样的测试,有什么建议吗
What you have done is wrong. You will have to mock the factory call.
spyOn(application,'createApp').and.callFake(function() {
return {
then : function(success) {
success({id: "abc"});
}
}
});
it('should return a new application', function(done) {
spyOn($state, 'go');
application.createApp();
expect($state.go).toHaveBeenCalled();
});