小编Tra*_*thy的帖子

Angular 7 Component Test使用的是原始服务而不是模拟

我正在尝试测试具有注入服务的组件。我想在测试中提供模拟服务。但是,测试使用的是原始服务而不是模拟服务(我知道这一点是因为我收到“ No Client for HttpClient!”错误,并且在测试中输出的原始服务中还有console.log)。

我可以通过导入HttpClientTestingModule来修复错误,但这不能解决使用原始服务而非模拟服务这一事实。

有什么想法我做错了吗?

这是我的测试代码。Angular版本7

import { TestBed, async, ComponentFixture } from '@angular/core/testing';
import { HelloWorldComponent } from '../../app/components/hello-world/hello-world.component';
import { HelloWorldService } from '../../app/services/hello-world.service';

describe('HelloWorldComponent', () => {

  let component: HelloWorldComponent;
  let fixture: ComponentFixture<HelloWorldComponent>;
  let mockHelloWorldService;
  
  beforeEach(() => {

    mockHelloWorldService = jasmine.createSpyObj(['getHelloWorld']);
    
    TestBed.configureTestingModule({
      imports: [],
      declarations: [HelloWorldComponent],
      providers: [
        [{ provide: HelloWorldService, useClass: mockHelloWorldService }]
      ]
    }).compileComponents();

    fixture = TestBed.createComponent(HelloWorldComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
	
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
  
});
Run Code Online (Sandbox Code Playgroud)

更新

我试过了overrideProvider,现在我遇到了“无法读取未定义的属性'subscribe'(未定义)”错误,感觉像是进度...

这是我的测试代码 …

unit-testing mocking angular

6
推荐指数
2
解决办法
2493
查看次数

未从 Azure Devops .NET 核心构建获得 SonarCloud 中的代码覆盖率

我已经使用“.NET Core with SonarCloud”模板在 Azure Devops 中为我的 .NET Core 项目设置了管道。当我构建时,分析会在 SonarCloud 中运行,但代码覆盖率为 0%(我的解决方案中有测试)。

无论我对构建进行什么配置调整,我都无法使代码覆盖率正常工作。

我错过了什么?

我遇到了这篇文章和https://dejanstojanovic.net/aspnet/2019/may/publishing-code-analysis-to-sonarcloud-from-azure-build-pipeline/ //dejanstojanovic.net/aspnet/2019/may/publishing-code-analysis-to-sonarcloud-from-azure-build-pipeline/ 实现了其中描述的powershell脚本,但我仍然没有SonarCloud 中的代码覆盖率

我尝试使用这里描述的床罩,但仍然没有乐趣 https://gunnarpeipman.com/aspnet/azure-devops-code-coverage/

我的管道由以下任务组成

  • .NET Core - 还原
  • 准备分析配置
  • .NET Core - 构建
  • .NET Core - 测试
  • 运行代码分析
  • 发布质量门结果

我的测试任务是这样配置的:

参数: --configuration $(BuildConfiguration)

发布测试结果和代码覆盖率 - 选中

在运行代码分析任务的控制台中,我得到:

10:43:54.7  Fetching code coverage report information from TFS...
10:43:54.702  Attempting to locate a test results (.trx) file...
10:43:54.753  Looking for TRX files in: C:\\TFSBuilds\\TJPYHG04-GHJ01\\_work\\475\\TestResults
10:43:54.755  No test results files found
10:43:54.81  Did not find …
Run Code Online (Sandbox Code Playgroud)

code-coverage .net-core azure-devops sonarcloud

6
推荐指数
2
解决办法
5576
查看次数