【问题标题】:Varaible declared is not defined in spec while testing测试时未在规范中定义声明的变量
【发布时间】:2017-06-18 13:05:26
【问题描述】:

这是我的实际 AzureService

declare var WindowsAzure;
declare var config;

@Injectable()
export class AzureService {
    azureUrl;
    client: any;
    programmes: Programme[];
    selectedProgramme: Programme;
    orderSummary: Order;

    constructor() {
        this.client = new WindowsAzure.MobileServiceClient(this.azureUrl);}
}

这是我的仪表板规格

describe('DashboardComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        DashbaordComponent,
        StatusComponent
      ],
      providers:[ AzureService]
    }).compileComponents();
  }));

  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(DashbaordComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

我在其中注入 azure 服务的仪表板组件承包商:

constructor(private azureService: AzureService) { }

测试失败,说明我没有定义 WindowAzure,但我想在哪里定义?

这是错误:

【问题讨论】:

    标签: angular karma-jasmine


    【解决方案1】:

    在模块级别声明变量

    declare var WindowsAzure;
    declare var config;
    @NgModule({...})
    

    以便它在组件、服务之间共享。

    也可以将它们导入到您使用的任何地方

    import { WindowsAzure } from '../azure.service.ts'
    

    【讨论】:

    • 谢谢@Aravind。实施后,无法读取未定义的属性MobileServiceClient
    • WindowsAzure 是您声明的变量。那么它将如何包含任何属性或方法。
    • WindowsAzure 我从<script src="//zumo.blob.core.windows.net/sdk/azure-mobile-apps-client.2.0.0.js"></script> 客户端获取它。我没有定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-14
    • 2020-04-12
    • 2019-12-04
    • 2013-03-24
    • 2017-07-14
    相关资源
    最近更新 更多