NativeScript-如何使单元测试正常工作? [英] NativeScript - how to get unit tests to work properly?

查看:58
本文介绍了NativeScript-如何使单元测试正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用NativeScript 6+和Angular 8+编写应用程序.

I'm writing an app with NativeScript 6+ and Angular 8+.

我正在尝试编写一些单元测试并使它们启动并运行.

I'm trying to write some unit tests and get them up and running.

我已经阅读了有关单元测试的文档: https://docs.nativescript.org/tooling/testing/testing

I have read the documentation on unit testing: https://docs.nativescript.org/tooling/testing/testing

我按照那里的指示进行设置并使用TestBed.我的测试无法正常工作并引发错误.

I followed the directions there for setting up the tests and using TestBed. My tests are not working and throwing errors.

这是我的存储库: https://github.com/aubrey-fowler/NativeScriptUnitTests

问题:

  1. 文档仅显示了如何为测试编写测试的示例零件.如何编写服务测试?我也需要用为此,请使用TestBed,因为我的服务具有依赖项注入.
  2. 我的测试抛出错误.为什么以及如何修复它们?

错误:

no reachable hosts

在我的Android手机上

on my Android phone

代码段:

import { ItemsComponent } from '../app/item/items.component';

import {
    nsTestBedAfterEach,
    nsTestBedBeforeEach,
    nsTestBedRender
} from 'nativescript-angular/testing';

describe('ItemsComponent Test', () => {

    beforeEach(nsTestBedBeforeEach([ItemsComponent]));
    afterEach(nsTestBedAfterEach(false));

    it('should be defined', () => {

        nsTestBedRender(ItemsComponent).then((fixture) => {
            fixture.detectChanges();
            const component = fixture.componentInstance;
            expect(component).toBeTruthy;
        });

    });

});

推荐答案

  1. 我检查了您的仓库,我不确定它是否是最新的,因为当我尝试运行 tns test [ios | android] 时,它崩溃了,我没有发现任何业力配置文件在回购中存在.我必须使用 tns test init 重新初始化测试,然后它才能正常工作.

  1. I checked your repo, I'm unsure whether it's upto date as when I tried to run tns test [ios|android] it broke and I noticed none of the karma config files were existed in repo. I had to re-initialise test with tns test init and then it worked just fine.

当您的组件具有依赖项时,您将必须将服务文件传递到providers数组中(将第二个参数传递给 nsTestBedBeforeEach 函数).如果要单独测试服务,则必须自己创建实例并运行测试.如果服务具有依赖项,则有责任在构造函数中传递依赖项.与在 Angular Web 中的操作相同.

When you have dependencies for your component, you will have to pass the Service files in the providers array (Second parameter to nsTestBedBeforeEach function). If you want to test the service alone, you will have to create the instance yourself and run tests. If the service has dependencies, it's your responsibility to pass the dependencies in the constructor. Just the same as how you would do in Angular Web.

这篇关于NativeScript-如何使单元测试正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆