小编cla*_*c89的帖子

用 Jasmine 在 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 …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing mocking spy angular

7
推荐指数
0
解决办法
2353
查看次数

标签 统计

angular ×1

javascript ×1

mocking ×1

spy ×1

unit-testing ×1