I have the following module:
angular.module('config', []).constant('myconstant', somevalue);
I would like to unit test this so I created:
describe('Constants', function () {
var config;
beforeEach( inject(function (_config_) {
module('config');
config =_config_;
}));
it('should return settings',function(){
expect(config.constant('myConstant')).toEqual('somevalue');
});
});
Getting an error now:
Error: [$injector:unpr] Unknown provider: configProvider <- config
How can I fix this?
You should be injecting your constant like any other service and not your module. This works for me:
angular.module('config', []).constant('myconstant', 'somevalue');
describe('Constants', function () {
var myconstant;
beforeEach(module('config'));
beforeEach( inject(function (_myconstant_) {
myconstant =_myconstant_;
}));
it('should return settings',function(){
expect(myconstant).toEqual('somevalue');
});
});
Related
I am testing a custom service made in angular JS where there is an error which is occurring. It is as follows:-
Error during loading: Uncaught ReferenceError: angular is not defined in http://localhost:63342/EmailTests/Unit%20Tests/jasmine-standalone-3.3.0/src/emailService.js line 3
Here is the code
describe('Email Service', function(){
var emailService;
beforeEach(function () {
module('bahmni.registration');
inject(function () {
emailService = $injector.get('emailService');
});
});
it('should have a valid Email ID',function () {
var parameters={
access_key : {parameter:'sdasdafassfa'},
smtp: {parameter:1}
};
var result=emailService.validateEmailService('https://apilayer.net/api/check',parameters,'someEmail');
expect(result.toEqual(true));
})
});
How can I solve this?
I have the following service with a constant:
angular.module('app',[]).constant('alertType',{
success:1,
error:0
})
.factory("dataService",dataService);
dataService.$inject = ['$timeout', 'alertType']
function dataService($timeout, alertType) {
return {
//some code related to the service
}
}
Here is the test case for the service to check if it is registered or not
describe('Testing "dataService" service', function() {
var _dataService;
beforeEach(function() {
module('app');
inject(function(dataService) {
_dataService = dataService;
});
});
it('Should be registered', function() {
expect(_dataService).toBeDefined();
});
});
For some reason it doesn't work. I get a very long error that looks something like this:
Error: [$injector:unpr] Unknown provider: dataServiceProvider <- dataService http://errors.angularjs.org/1.3.0/$injector/unpr?p0=dataServiceProvider%20%3C-%20dataService
Am I doing it right or is there anything wrong with this?
Try mocking your provider at the top of your describe block like so:
beforeEach(module($provide => {
$provide.constant('alertType',{
success: 1,
error: 0
});
}));
For more details please refer to this answer: Unknown Provider when unit testing filter
Given the app startup:
angular.module("starter", [ "ionic" ])
.constant("DEBUG", true)
.run(function() {
/* ... */
});
how would I test the value of DEBUG?
When trying with:
describe("app", function() {
beforeEach(function() {
module("starter");
});
describe("constants", function() {
describe("DEBUG", inject(function(DEBUG) {
it("should be a boolean", function() {
expect(typeof DEBUG).toBe("boolean");
});
}));
});
});
I just get
TypeError: 'null' is not an object (evaluating 'currentSpec.$modules')
at workFn (/%%%/www/lib/angular-mocks/angular-mocks.js:2230)
at /%%%/www/js/app_test.js:14
at /%%%/www/js/app_test.js:15
at /%%%/www/js/app_test.js:16
Make sure it is being instantiated in the right place.
In this case, the beforeEach was not being run to load the module, because DEBUG was being inject()ed in the describe block, not the it block. The following works properly:
describe("app", function() {
var DEBUG;
beforeEach(function() {
module("starter");
});
describe("constants", function() {
describe("DEBUG", function() {
it("should be a boolean", inject(function(DEBUG) {
expect(typeof DEBUG).toBe("boolean");
}));
});
});
});
Simple way to inject your existing constants into your karma tests.
// Assuming your constant already exists
angular.module('app').constant('STACK', 'overflow')...
// Your Karma Test Suite
describe('app', function() {
var STACK;
beforeEach(module('APP'));
beforeEach(inject(function ($injector) {
STACK = $injector.get('STACK');
}));
// Tests...
});
Why when I run this in the karma runner:
describe('Service tests', function () {
var DataServiceMock
var httpBackend;
beforeEach(angular.mock.module('app'));
beforeEach(angular.mock.inject(function( $httpBackend, $service, DataService, $injector){
results in this error
Error: [$injector:unpr] Unknown provider: $serviceProvider <- $service
http://errors.angularjs.org/1.2.1/$injector/unpr?p0=%24serviceProvider%20%3C-%20%24service
at /home/site/angular/angular.js:78:12
Edit 2____________________
I'm trying to mock the DataService for the dataHandlerService
describe('Service', function () {
var DataServiceMock
var httpBackend;
var testUrl = "test/";
beforeEach(angular.mock.module('app'));
beforeEach(angular.mock.inject(function(){
module(function ($provide) {
$provide.value('DataService', DataServiceMock)
})
}));
it('should have', inject(function(DataService) {
expect('DataService').not.toBe(null);
}));
and this error:
Error: Injector already created, can not register a module!
at workFn (/home/me/root/angular/angular-mocks.js:1985:15)
There is no $service. If you want to mock your service then you should do it by using $provide:
beforeEach(function () {
DataServiceMock= {}
DataServiceMock.doSomething = function() {}
module(function ($provide) {
$provide.value('DataService', DataServiceMock)
})
})
I am having a lot of trouble getting dependencies provided properly for an AngularJS service.
I see a number of other posts with similar errors here on StackOverflow but none of them seem to resolve the issue.
Here is the app code:
cm.modules.app = angular.module('myApp', ['ngRoute', 'ngAnimate']);
myServiceName = function($http) {
// do stuff
};
myServiceName.prototype.value = 1;
cm.modules.app.service('defaultAlertFactoryA', myServiceName);
Here is the test code:
describe('test alertFactoryA', function() {
var $provide;
var mAlertFactoryA;
beforeEach(module(cm.modules.app));
beforeEach(angular.mock.module(function(_$provide_) {
$provide = _$provide_;
}));
beforeEach(function() {
inject(function($injector) {
mAlertFactoryA = $injector.get('defaultAlertFactoryA');
});
});
it('should work', function() {
expect(true).toBe(true);
});
});
Here is the error:
Error: [$injector:unpr] Unknown provider: defaultAlertFactoryAProvider
<- defaultAlertFactoryA
http://errors.angularjs.org/1.2.0-rc.2/$injector/unpr?p0=defaultAlertFactoryAProvider%20%3C-%20defaultAlertFactoryA
Question: How do I fix this so the test passes?
In order to bootstrap your module you need to provide its name
beforeEach(module('myApp'));
Demo
The following is what I used to get it working (finally)
beforeEach(function() {
module(cm.modules.app.name);
module(function($provide) {
$provide.service('defaultAlertFactoryA', myServiceName);
});
inject(function($injector) {
defaultAlertFactory = $injector.get('defaultAlertFactoryA');
});
});
Sounds like you need to include the service files in your karma.conf.js file
files: [
'app/bower_components/angular/angular.js',
'app/bower_components/angular-mocks/angular-mocks.js',
'app/bower_components/angular-ui-router/release/angular-ui-router.js',
'app/app.js',
'app/controllers/*.js',
'app/services/*.js',
'tests/**/*.js'
],
If the are not included here they can't be accessed in the unit tests