Max*_*ach 53 unit-testing jasmine angularjs
有没有人有一个如何对提供商进行单元测试的例子?
例如:
config.js
angular.module('app.config', [])
.provider('config', function () {
var config = {
mode: 'distributed',
api: 'path/to/api'
};
this.mode = function (type) {
if (type) {
config.isDistributedInstance = type === config.mode;
config.isLocalInstance = !config.isDistributedInstance;
config.mode = type;
return this;
} else {
return config.mode;
}
};
this.$get = function () {
return config;
};
}]);
Run Code Online (Sandbox Code Playgroud)
app.js
angular.module('app', ['app.config'])
.config(['configProvider', function (configProvider) {
configProvider.mode('local');
}]);
Run Code Online (Sandbox Code Playgroud)
app.js在测试中使用,我看到已配置configProvider,我可以将其作为服务进行测试.但是我如何测试配置能力?或者它根本不需要?
Mar*_*ill 64
我有同样的问题,只在这个Google Group答案中找到了一个有效的解决方案,它引用了小提琴的例子.
测试你的提供者代码看起来像这样(遵循小提琴示例中的代码以及对我有用的东西):
describe('Test app.config provider', function () {
var theConfigProvider;
beforeEach(function () {
// Initialize the service provider
// by injecting it to a fake module's config block
var fakeModule = angular.module('test.app.config', function () {});
fakeModule.config( function (configProvider) {
theConfigProvider = configProvider;
});
// Initialize test.app injector
module('app.config', 'test.app.config');
// Kickstart the injectors previously registered
// with calls to angular.mock.module
inject(function () {});
});
describe('with custom configuration', function () {
it('tests the providers internal function', function () {
// check sanity
expect(theConfigProvider).not.toBeUndefined();
// configure the provider
theConfigProvider.mode('local');
// test an instance of the provider for
// the custom configuration changes
expect(theConfigProvider.$get().mode).toBe('local');
});
});
});
Run Code Online (Sandbox Code Playgroud)
jam*_*mes 46
我一直在使用@Mark Gemmill的解决方案并且运行良好,但随后偶然发现了这个稍微不那么冗长的解决方案,无需使用假模块.
所以,
var provider;
beforeEach(module('app.config', function(theConfigProvider) {
provider = theConfigProvider;
}))
it('tests the providers internal function', inject(function() {
provider.mode('local')
expect(provider.$get().mode).toBe('local');
}));
Run Code Online (Sandbox Code Playgroud)
如果您的providers $ get方法有依赖关系,您可以手动传递它们,
var provider;
beforeEach(module('app.config', function(theConfigProvider) {
provider = theConfigProvider;
}))
it('tests the providers internal function', inject(function(dependency1, dependency2) {
provider.mode('local')
expect(provider.$get(dependency1, dependency2).mode).toBe('local');
}));
Run Code Online (Sandbox Code Playgroud)
或者使用$ injector创建一个新实例,
var provider;
beforeEach(module('app.config', function(theConfigProvider) {
provider = theConfigProvider;
}))
it('tests the providers internal function', inject(function($injector) {
provider.mode('local')
var service = $injector.invoke(provider);
expect(service.mode).toBe('local');
}));
Run Code Online (Sandbox Code Playgroud)
以上两者还允许您为块中的每个单独it语句重新配置提供程序describe.但是,如果您只需要为多个测试配置一次提供程序,则可以执行此操作,
var service;
beforeEach(module('app.config', function(theConfigProvider) {
var provider = theConfigProvider;
provider.mode('local');
}))
beforeEach(inject(function(theConfig){
service = theConfig;
}));
it('tests the providers internal function', function() {
expect(service.mode).toBe('local');
});
it('tests something else on service', function() {
...
});
Run Code Online (Sandbox Code Playgroud)