我正在尝试获取我的角度项目的代码覆盖率。我不太熟悉这些工具。我决定使用"istanbul-instrumenter-loader": "^3.0.1". 我尝试从这个问题中寻求帮助:
以及在同一线程上给出的许多其他解决方案。我的问题是我想排除我为单元测试编写的规范文件。这是我得到的屏幕截图: 截图。请纠正我的错误,并随时询问缺失的信息。
我正在学习在AngularJS中实现karma&Jasmine,我正在通过它的一些例子来更好地理解它.
我有点困惑callThrough.
如果我误解了它,请纠正我,它与callFake()我们实际上没有调用函数的方式略有相似.在callFake()我们还提供与返回类型,但不是在一个函数callThrough.
来自Jasmine doc:
通过使用and.callThrough链接间谍,间谍仍将跟踪对它的所有调用,但此外它将委托给实际的实现.
请详细说明一下.
谢谢
我正在为角度应用程序运行单元测试,我尝试从单元测试内的 .css 访问样式。我会让你知道我尝试了什么
component.listedIps.length=0;
fixture.detectChanges();
let whitelistipsdiv=fixture.debugElement.query(By.css('.divAllAllowedIPs'));
//unit test for style, test for background-color to be green
expect(whitelistipsdiv.nativeElement.style.backgroundColor).toEqual('green');// to be darkgreen, here it is null
Run Code Online (Sandbox Code Playgroud)
.css 文件
.divAllAllowedIPs {
background-color: green;
}
Run Code Online (Sandbox Code Playgroud) 我使用一个组件作为通过路由到达的常规组件,但我希望它也将它用作模态对话框的“目标”,当使用注入组件时:
export class Component1 implements OnInit {
constructor(private service: <someService>,
public dialogRef: MatDialogRef<Component1>, //These 2 lines are used as
//injection from the opener
@Inject(MAT_DIALOG_DATA) public data: any) {}
Run Code Online (Sandbox Code Playgroud)
这是“开瓶器”的代码:
openComponent1aSModalPage()
{
Const dialogRef = this.dialog.open(Component1, {
width: '70%',
height: '70%',
data: {property: propertyValue}
});
Run Code Online (Sandbox Code Playgroud)
我在激活开启器时工作,但如果我尝试使用常规路由到达相同的组件,我会得到:
错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)[Component1 -> MatDialogRef]:StaticInjectorError(Platform:core)[Component1-> MatDialogRef]:NullInjectorError:没有 MatDialogRef 的提供者!
如何调整该组件以在两种模式下工作?
dependency-injection modal-dialog typescript angular-material angular
我有一个订单表..每个表行代表一个订单实体,当用户单击特定行时,它应该重定向到包含单击订单详细信息的新视图。这两个组件不是父级和子级,我想将有关单击的订单行的数据传递到导航页面。我尝试了以下方法
<tr *ngFor="let order of allOrders" routerLink="../orderdetails" [state]=”{data:order}”>
<td>{{order.orderRef}}</td>
<td>{{order.receiptRef}}</td>
<td>{{order.customer}}</td>
<td>{{order.orderDate}}</td>
<td>{{order.salesMan}}</td>
<td>{{order.total}}</td>
<td>{{order.status}}</td>
<td>{{order.session}}</td>
<td>
</td>
</tr>
Run Code Online (Sandbox Code Playgroud)
这会导致视图导航到 Orderdetails
ngOnInit(): void {
console.log(history.state)
console.log(history.state.data);
}
Run Code Online (Sandbox Code Playgroud)
在 ngOnInit()console.log(history.state)日志中,{navigationId: 1}
但console.log(history.state.data)logs undefined.
为什么我得到的undefined不是我在 上设置的数据state?
如何访问我传递给state属性的数据?
我正在使用<c:forEach>迭代List如下:
<c:forEach items="${list}" var="item">
${item}
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
如何跳过列表中第一项的打印?
我正在使用以下功能打开Mat对话框:
accept() {
let dialogRef = this.dialog.open(AcceptDialogComponent, {
data: {
hasAccepted: false
}
})
dialogRef.afterClosed().subscribe(result => {
console.log(result);
if (result.hasAccepted === true) {
this.leadService.acceptLead(this.holdingAccountId, this.lead.id)
.pipe(
takeUntil(this.onDestroy$)
)
.subscribe (acceptLeadRes => {
console.log(acceptLeadRes);
this.leadService.updateLeadAction('accept');
},
(err: HttpErrorResponse) => {
console.log(err);
this.router.navigate(['/error']);
});
}
});
}
Run Code Online (Sandbox Code Playgroud)
我试图为此功能编写一个测试,该测试仅触发afterClosed(),以便可以检查是否调用了我的进行后端调用的服务方法。
component.spec.ts(在创建每个测试平台之前)
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ LeadCardComponent, AcceptDialogComponent ],
imports: [
requiredTestModules,
JwtModule.forRoot({
config: {
tokenGetter: () => {
return '';
}
}
})
],
providers: [
ApplicationInsightsService,
JwtHelperService,
// { provide: LeadsService, …Run Code Online (Sandbox Code Playgroud) 在这里输入图像描述我最近向package.JSON添加了一些依赖项,并尝试安装npm。但它显示与git相关的错误,也找不到
npm ERR!路径git
npm错误!代码ENOENT
npm ERR!errno ENOENT
npm ERR!syscall生成git
npm ERR!enoent执行时出错:
npm ERR!enoent未定义ls-remote -h -t ssh://git@github.com/eligrey/FileSaver.js.git
npm ERR!enoent
npm ERR!enoent
npm ERR!enoent生成git ENOENT
npm ERR!enoent这与npm无法找到文件有关。npm ERR!enoent
npm ERR!可以在以下位置找到此运行的完整日志:
尝试运行 karma TS 规范文件时出现错误。所有模块和导入似乎都工作正常,没有冲突。尝试添加component.ngOninit()到 beforeEach() 和 it() 中,但没有取得任何成功。查看类似的问题与 HttpClientTestingModule 导入冲突或其他提供程序/声明冲突有关。我无法确定为什么 TypeError: Cannot read property 'get' of undefined 不允许此规范文件通过
规格文件
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {AcaPersonComponent} from './aca-person.component';
import {
MatCardModule, MatDividerModule,
MatFormFieldModule,
MatIconModule,
MatInputModule,
MatOptionModule,
MatRadioModule,
MatSelectModule
} from '@angular/material';
import {ReactiveFormsModule} from '@angular/forms';
import {MFGDatePickerModule} from 'mfg-datepicker';
import {TextMaskModule} from 'angular2-text-mask';
import {AppRoutingModule} from '../../../../../app-routing.module';
import {HttpClientTestingModule} from '@angular/common/http/testing';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {LandingPageComponent} from '../../../../../landing-page/landing-page.component';
describe('AcaPersonComponent', () => {
let component: AcaPersonComponent;
let …Run Code Online (Sandbox Code Playgroud) 我是 Angular 单元测试的新手,正在查看其他人编写的一些测试。我看到尝试以三种不同的方式访问元素:
fixture.debugElement.query(By.css('#hello'))fixture.debugElement.nativeElement.querySelector('#hello')document.getElementById('#hello')使用这些有什么区别/最佳实践吗?
angular ×8
jasmine ×4
typescript ×4
unit-testing ×4
angular-test ×1
angular7 ×1
angularjs ×1
css ×1
foreach ×1
git ×1
html ×1
istanbul ×1
javascript ×1
jstl ×1
list ×1
modal-dialog ×1
npm ×1
skip ×1