typescript 运行 tslint 时,角度单元测试中的“未使用的表达式,需要赋值或函数调用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44265207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:35:44  来源:igfitidea点击:

'unused expression, expected an assignment or function call' in an angular unit test when running tslint

angulartypescriptlint

提问by bobbyrne01

Is there a way to fix this error without having to place an ignore in the file?

有没有办法解决这个错误而不必在文件中放置一个忽略?

Error when running command:

运行命令时出错:

./node_modules/tslint/bin/tslint -p src/tsconfig.json --type-check src/app/app.component.spec.ts
[21, 5]: unused expression, expected an assignment or function call

Test:

测试:

let chai = require('chai');
let expect = chai.expect;

import { AppComponent } from './app.component';

import { ComponentFixture, TestBed } from '@angular/core/testing';

describe('AppComponent', function() {
  let testAppComponent: AppComponent;
  let fixture: ComponentFixture<AppComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      declarations: [AppComponent]
    });
    fixture = TestBed.createComponent(AppComponent);
    testAppComponent = fixture.componentInstance;
  });

  it('should create the correct component', () => {
    expect(testAppComponent instanceof AppComponent).to.be.true;
  });
});

采纳答案by Duncan

Did you perhaps mean this:

你可能是这个意思:

expect(testAppComponent instanceof AppComponent).toBe(true);

The expression you have there just accesses a bunch of properties, you need some kind of function call for it to actually do anything.

您在那里拥有的表达式只是访问一堆属性,您需要某种函数调用才能真正执行任何操作。