首页 > 其他分享 >无涯教程-Angular7 - 测试和构建

无涯教程-Angular7 - 测试和构建

时间:2023-12-08 20:31:28浏览次数:28  
标签:教程 const component app fixture 无涯 AppComponent ts Angular7

本章将讨论以下内容-

  • To Test Angular 7 Project
  • To Build Angular 7 Project

测试Angular 7项目

在项目设置过程中,已经安装了所需的测试软件包。为每个新组件,服务,指令等创建一个 .spec.ts 文件。

要运行测试用例,使用的命令如下-

ng test

以下是 app.component.ts 的app.component.spec.ts文件-

import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
   beforeEach(async(() => {
      TestBed.configureTestingModule({
         imports: [
            RouterTestingModule
         ],
         declarations: [
            AppComponent
         ],
      }).compileComponents();
   }));
   it('should create the app', () => {
      const fixture=TestBed.createComponent(AppComponent);
      const app=fixture.debugElement.componentInstance;
      expect(app).toBeTruthy();
   });
   it(`should have as title 'angular7-app'`, () => {
      const fixture=TestBed.createComponent(AppComponent);
      const app=fixture.debugElement.componentInstance;
      expect(app.title).toEqual('angular7-app');
   });
   it('should render title in a h1 tag', () => {
      const fixture=TestBed.createComponent(AppComponent);
      fixture.detectChanges();
      const compiled=fixture.debugElement.nativeElement;
      expect(compiled.querySelector('h1').textContent).toContain(
         'Welcome to angular7-app!');
   })
});

app.component.ts

import { Component } from '@angular/core';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title='angular7-app';
}

现在,让我们运行命令以查看测试用例。

Ng TestRun The Command

测试用例的状态显示在命令行上,如上所示,并且还将在浏览器中打开,如下所示-

Karma

如果发生任何报错,它将显示以下详细信息:

为此,让我们如下更改app.component.spec.ts-

import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
   beforeEach(async(() => {
      TestBed.configureTestingModule({
         imports: [
            RouterTestingModule
         ],
         declarations: [
            AppComponent
         ],
      }).compileComponents();
   }));
   it('should create the app', () => {
      const fixture=TestBed.createComponent(AppComponent);
      const app=fixture.debugElement.componentInstance;
      expect(app).toBeTruthy();
   });
   it(`should have as title 'angular7-app'`, () => {
      const fixture=TestBed.createComponent(AppComponent);
      const app=fixture.debugElement.componentInstance;
      expect(app.title).toEqual('Angular 7'); //change the 
      title from angular7-app to Angular 7
   });
   it('should render title in a h1 tag', () => {
      const fixture=TestBed.createComponent(AppComponent);
      fixture.detectChanges();
      const compiled=fixture.debugElement.nativeElement;
      expect(compiled.querySelector('h1').textContent).toContain(
         'Welcome to angular7-app!');
   });
});

在上面的文件中,测试用例检查标题 Angular 7 。但是在app.component.ts中,我们有标题 angular7-app ,如下所示-

import { Component } from '@angular/core';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title='angular7-app';
}

这里的测试用例将失败,下面是命令行和浏览器中显示的详细信息。

命令行中显示以下屏幕-

Command Line

浏览器中显示以下屏幕-

Karma Connected

项目的所有失败测试用例都将在命令行和浏览器中显示,如上所示。

构建Angular 7项目

在Angular中完成项目后,我们需要对其进行构建,以便将其用于生产环境中。

需要在 src/environments 中定义构建配置,即production,staging,development,testing。

目前,我们在src/environment中定义了以下环境-

Environments

您可以将基于构建的文件添加到src/environment中,即environment.staging.ts,enviornment.testing.ts等。

export const environment={
   production: false
};

要生成用于生产的文件,我们需要在environment.ts中使 production:true 如下:

export const environment={
   production: true
};

必须将默认环境文件导入组件内部,如下所示:

app.component.ts

import { Component } from '@angular/core';
import { environment } from './../environments/environment';

@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   title='angular7-app';
}

我们试图做的从默认环境到生产环境的替换是在angular.json fileReplacements 部分中定义的,如下所示-

"production": {
   "fileReplacements": [
      {
         "replace": "src/environments/environment.ts",
         "with": "src/environments/environment.prod.ts"
      }
   ],
}

运行构建命令后,文件将替换为 src/environments/environment.prod.ts 。如下面的示例所示,可以在此处添加诸如登台或测试之类的其他配置-

"configurations": {
   "production": { ... },
   "staging": {
      "fileReplacements": [
         {
            "replace": "src/environments/environment.ts",
            "with": "src/environments/environment.staging.ts"
         }
      ]
   }
}

所以运行构建的命令如下-

ng build --configuration=production //for production environmnet
ng build --configuration=staging //for stating enviroment

现在让我们运行build命令进行生产,该命令将在我们的项目中创建一个dist文件夹,该文件夹将在构建后包含最终文件。

Ng BuildDist Folder

最终文件构建在dist /文件夹中,该文件夹可以托管在最终的生产服务器上。

Dist

参考链接

https://www.learnfk.com/angular7/angular7-testing-and-building-project.html

标签:教程,const,component,app,fixture,无涯,AppComponent,ts,Angular7
From: https://blog.51cto.com/u_14033984/8741671

相关文章

  • 无涯教程-Angular7 - Materials模块
    Materials为您的项目提供了许多内置模块。autocomplete,datepicker,slider,menus,grids和toolbar等函数。要使用Materials,我们需要导入包装。Angular2也具有上述所有函数,但可以作为@angular/core模块的一部分使用。从Angular4开始,Materials模块提供了一个单独的模块@angular/mat......
  • 无涯教程-Angular7 - 动画效果
    Animations在html元素之间增加了很多交互,Angular2可以使用动画,从Angular4开始,动画不再是@angular/core库的一部分,而是一个单独的程序包,需要将其导入app.module.ts中。首先,我们需要使用下面的代码行导入库-import{BrowserAnimationsModule}from'@angular/platform-browse......
  • 无涯教程-Angular7 - Http Client
    HttpClient将帮助我们提供POST,GET相关方法,使用时需要导入http模块。我们需要将模块导入app.module.ts中,如下所示-import{BrowserModule}from'@angular/platform-browser';import{NgModule}from'@angular/core';import{AppRoutingModule,RoutingComponent}from......
  • DIY鱼缸制作教程
    鱼缸玻璃的连接方式:边包底...鱼缸5块玻璃的尺寸计算:使用在线工具:http://www.sexy0769.com/yugangdiy/index.html直接计算鱼缸玻璃选择首先说明:不能用钢化玻璃,钢化玻璃不稳定,容易自爆,不适合用在鱼缸上。普通浮法玻璃鱼缸爆的时候有裂缝,有补救或者倒水的时间,钢化玻璃爆会瞬间......
  • 无涯教程-Angular7 - 路由(Routing)
    路由(Routing)基本上意味着在页面之间导航,现在让我们创建一个组件,看看如何在其上使用路由。如下所示-app.module.tsimport{BrowserModule}from'@angular/platform-browser';import{NgModule}from'@angular/core';import{AppRoutingModule}from'./app-routin......
  • 静态HTTP的基础知识:菜鸟的教程与指南
    大家好,今天我要给大家讲解一个非常基础但重要的知识点——静态HTTP。如果你是一位初入互联网的小白,对于HTTP这个缩写可能还有些陌生。没关系,今天我们就来揭开它的神秘面纱。首先,让我们想象一下,当你在浏览器中输入一个网址,比如www.example.com,你的浏览器就开始和这个网站的服务器进......
  • 界面控件DevExpress中文教程 - 如何用Office File API组件填充PDF表单
    DevExpressOfficeFileAPI是一个专为C#,VB.NET和ASP.NET等开发人员提供的非可视化.NET库。有了这个库,不用安装MicrosoftOffice,就可以完全自动处理Excel、Word等文档。开发人员使用一个非常易于操作的API就可以生成XLS,XLSx,DOC,DOCx,RTF,CSV和SnapReport等企业级文......
  • 无涯教程-Angular7 - 事件绑定
    在本章中,无涯教程将讨论事件绑定在Angular7中的工作方式,当用户以键盘移动,鼠标单击或鼠标悬停的形式与应用程序交互时,它将生成一个事件。需要处理这些事件以执行某种操作,让无涯教程考虑一个示例以更好地理解这一点。app.component.html<!--Thecontentbelowisonlyaplace......
  • Qt6 c++教程1简介
    1Qt6简介Qt一个跨平台应用程序开发框架,旨在为桌面、嵌入式和移动平台创建具有统一用户界面(UI)的优秀软件应用程序。它为开发人员设计和构建优秀应用程序提供了一套强大的工具,而无需担心平台依赖性。本章主要内容:Qt介绍使用Qt的原因下载和安装Qt从源代码构建Qt61.1Qt简......
  • 无涯教程-Angular7 - 简介
    Angular7是一个开放源代码JavaScript框架,用于在JavaScript, html 和Typescript(JavaScript的超集)中构建Web应用程序和应用程序。Angular为animation,httpservice和materials提供了内置功能,这些功能又具有auto-complete,naigation,toolbar,menus等功能。代码以Typescript编写......