【问题标题】:Angular6 Jasmine TypeError: expect(...).toBeVisible is not a functionAngular6 Jasmine TypeError:expect(...).toBeVisible 不是函数
【发布时间】:2018-07-22 09:09:42
【问题描述】:

在 angular6 中设置 jasmine-query-matches

在 Angular 5 项目中,它看起来很简单

import { } from 'jasmine-jquery/lib/jasmine-jquery';
import { } from 'jasmine-jquery-matchers';
import * as $ from 'jquery';

在 Angular 6 上我尝试了以下

import {} from "jasmine-jquery/lib/jasmine-jquery" ;
import {} from "jasmine-jquery-matchers/dist/jasmine-jquery-matchers" ;
import { } from "karma-jasmine-jquery";
import * as $ from 'jquery';

import {} from "jasmine-jquery" ;
import {} from "jasmine-jquery-matchers" ;
import { } from "karma-jasmine-jquery";
import * as $ from 'jquery';

用法如下

imagesEL = fixture.debugElement.query(By.css('.cycle'));

expect(imagesEL).toBeVisible();

expect(imagesEL.nativeElement).toBeVisible();

但是每件事似乎都给出了同样的错误

TypeError: expect(...).toBeVisible 不是函数

感谢您的帮助

Sample Code : https://stackblitz.com/edit/ng-test-tobevisible?file=app/hello.component.spec.ts

【问题讨论】:

  • @amit-chigadani 试过 nativeElement 不工作

标签: angular jasmine karma-jasmine angular6 jasmine-jquery


【解决方案1】:

尝试将其用作您的导入

import "jasmine-jquery/lib/jasmine-jquery" ;
import "jasmine-jquery-matchers/dist/jasmine-jquery-matchers" ;
import "karma-jasmine-jquery";
import 'jquery';

请注意 jasmine-jquery-matchers 没有默认导出

【讨论】:

    【解决方案2】:

    toBeVisible() 应该应用于nativeElement

    imagesEL = fixture.debugElement.query(By.css('.cycle'));
    expect(imagesEL.nativeElement).toBeVisible();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      相关资源
      最近更新 更多