【发布时间】:2016-06-14 01:59:38
【问题描述】:
我在单元测试 $log 时遇到了一些挑战。
我想尝试一个简单的测试,该测试采用我注入和测试的值。我还没有将规范与一个宁静的电话联系起来。我正在使用角度模拟。这是我之前的每个陈述。我有通过角度模拟定义的模块。我一直在测试的一些内容来自这个博客 http://www.bradoncode.com/blog/2015/06/08/ngmock-fundamentals-log/.
在我的 devDependencies 中
"angular": "^1.5.0",
"angular-mocks": "^1.5.0",
我正在使用 gulp 来设置测试。
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();
});
});
所以我知道 angular 和 mocks 的版本是匹配的。
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();
});
测试本身只是确保我有 $log 工作。
it('should call logs', function () {
$log.info('it worked');
expect($log.info.logs).toContain(['it worked']);
});
但是我要回来了
ReferenceError: inject is not defined
************* 更新 ***********
我确实在模拟模块中设置了 $log
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);
});
当我运行 gulp 测试时,我在 console.log($log) 中得到了我想要的输出。但是,$log 仍然返回
ReferenceError: $log is not defined
在规范中。
【问题讨论】:
-
这是完整的文件吗?你定义了任何
describe函数吗? -
是的,我在描述服务 describe('haHttpService', function () {
-
为了节省空间,我省略了一些其他测试