cla*_*c89 7 javascript unit-testing mocking spy angular
我仍在学习如何使用 Karma/Jasmine 编写角度测试。我想为环境的 isDebugMode 返回 false。当我更改它的代码时,它显然有效,但是我如何为此编写测试?我已经设置了一些测试,试图找出如何模拟环境。但似乎没有一个像我认为的那样工作。我想知道是否有人可以解释为什么这些会返回他们所做的值。
import { TestBed, inject } from '@angular/core/testing';
import { LoggerService } from './logger.service';
import { environment } from '../environments/environment';
import { mockEnvironment} from '../environments/environment.mock';
describe('Test', () => {
beforeEach(() => {
spyOn(console, 'error');
spyOn(console, 'warn');
TestBed.configureTestingModule({
providers: [ LoggerService ]
});
});
it('should be created',
inject([LoggerService],
(service: LoggerService) => {
expect(service).toBeTruthy();
}));
it('should always work', function() {
expect(true).toBe(true);
});
it('is the mocked environment returning false', function() {
spyOn(mockEnvironment, 'isDebugMode');
expect(mockEnvironment.isDebugMode).toEqual(false);
});
it('is the environment returning true', function() {
spyOn(environment, 'isDebugMode');
expect(environment.isDebugMode).toEqual(true);
});
it('enclosing in function makes everything true, why?',
function () {
spyOn(environment, 'isDebugMode').and.returnValue(false);
inject([LoggerService],
(service: LoggerService) => {
const type = 'warn';
const message = 'This message';
service.log(type, message);
expect(console.warn).not.toHaveBeenCalled(); // this does not seem to matter
});
}
);
it('enclosing in function makes everything true, why?',
function () {
spyOn(environment, 'isDebugMode').and.returnValue(false);
inject([LoggerService],
(service: LoggerService) => {
const type = 'warn';
const message = 'This message';
service.log(type, message);
expect(console.warn).toHaveBeenCalled(); // this does not seem to matter
});
}
);
it('should show up if isDebugMode is true',
inject([LoggerService],
(service: LoggerService) => {
const type = 'warn';
const message = 'This message';
service.log(type, message);
expect(console.warn).toHaveBeenCalled(); // this works
})
);
it('should not show up if isDebugMode is false using spy with return value',
inject([LoggerService],
(service: LoggerService) => {
const type = 'warn';
const message = 'This message';
spyOn(environment, 'isDebugMode').and.returnValue(false);
service.log(type, message);
expect(console.warn).not.toHaveBeenCalled(); // this is does not work
})
);
it('should not show up if isDebugMode is false using spy with callfake',
inject([LoggerService],
(service: LoggerService) => {
const type = 'warn';
const message = 'This message';
spyOn(environment, 'isDebugMode').and.callFake(mockEnvironment.isDebugMode); // boolean is not a function
service.log(type, message);
expect(console.warn).not.toHaveBeenCalled(); // this is does not work
})
);
it('should not show up if isDebugMode is false using spy with stub',
inject([LoggerService],
(service: LoggerService) => {
const type = 'warn';
const message = 'This message';
spyOn(environment, 'isDebugMode').and.stub();
service.log(type, message);
expect(console.warn).not.toHaveBeenCalled(); // this is does not work
})
);
});
Run Code Online (Sandbox Code Playgroud)
这些是我要测试的文件:logger.service.ts
import { Injectable } from '@angular/core';
import { environment } from '../environments/environment';
const isDebugMode = environment.isDebugMode;
@Injectable()
export class LoggerService {
log(type: string, message?: any): void {
if (type === 'info' || type === 'warn' || type === 'debug' || type === 'error' || type === 'log') {
const logFn: Function = (console)[type];
if (isDebugMode) {
logFn.apply(console, [message]);
}
} else {
console.error('Logger type incorrect');
}
}
}
Run Code Online (Sandbox Code Playgroud)
随着环境.ts
export const environment = {
production: false,
hmr: false,
isDebugMode: true
};
Run Code Online (Sandbox Code Playgroud)
和 environment.mock.ts
export const MockEnvironment = {
production: false,
hmr: false,
isDebugMode: false
};
Run Code Online (Sandbox Code Playgroud)
测试
| 归档时间: |
|
| 查看次数: |
2353 次 |
| 最近记录: |