I am running into some challenges unit testing $log.
I wanted to try a simple test that took the value I injected and tested. I am not hooking the spec up to a restful call yet. I am using angular mocks. Here are my before each statements. I have the module defined through angular mocks. Some of what I have been testing has came from this blog http://www.bradoncode.com/blog/2015/06/08/ngmock-fundamentals-log/ .
In my devDependencies
"angular": "^1.5.0",
"angular-mocks": "^1.5.0",
I am using gulp to set up the tests.
gulp.task('test:jasmine', function (done) { // move to return async when execution metrics done
gulp.src('./Scripts/tests/modules/utility/services/ha-http.service.jasmine.test.js')
.pipe(jasmine({
verbose: true
}))
.on('error', gutil.log)
.on('end', function () {
console.log('jasmine end');
done();
});
});
So I know the versions of angular and mocks are matched up.
beforeEach(function () {
angular.mock.module('ha.module.utility');
angular.mock.inject(function ($httpBackend, haHttpService) {
http = $httpBackend;
service = haHttpService;
});
});
beforeEach(inject(function (_$log_) {
$log = _$log_;
}));
afterEach(function () {
http.flush();
http.verifyNoOutstandingExpectation();
http.verifyNoOutstandingRequest();
});
The test itself is just making sure I have $log working.
it('should call logs', function () {
$log.info('it worked');
expect($log.info.logs).toContain(['it worked']);
});
However I am returning
ReferenceError: inject is not defined
************* Update ***********
I did set up $log in the mock module
angular.mock.module('ha.module.utility', function ($provide) {
$provide.decorator('$log', function ($delegate) {
return $delegate;
});
});
angular.mock.inject(function ($httpBackend, haHttpService, $log) {
http = $httpBackend;
service = haHttpService;
console.log($log);
});
I am getting the output I want in the console.log($log) when I run my gulp test. However, $log still returns
ReferenceError: $log is not defined
in the spec.
ReferenceError: inject is not defined
means that inject
global was not defined, literally. It is defined in angular-mocks.js on condition .
The problem is caused by testing rig and not the specs. angular-mocks.js may not be loaded, or it may be loaded before Jasmine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.