我们可以将 TestBed 用于带有 NativeScript 的服务吗?
Can we use TestBed for services with NativeScript?
我正在使用 NativeScript 6+ 和 Angular 8+ 编写应用程序。
我正在尝试编写一些单元测试并启动它们 运行。
我已阅读有关单元测试的文档:https://docs.nativescript.org/tooling/testing/testing
我进行了一些单元测试和 运行 普通服务。
例如
import { ItemService } from '../app/item/item.service';
describe('ItemsService Test', () => {
let service: ItemService;
beforeEach(() => { service = new ItemService(); });
it('should be defined',() => {
expect(service).toBeTruthy();
});
it('should have its functions defined',() => {
expect(service.getItems).toBeTruthy();
expect(service.getItem).toBeTruthy();
});
});
我看到我们可以通过在构造函数和模拟服务中实例化它们来测试具有依赖性的服务。
https://angular.io/guide/testing#service-tests
是否可以在 NativeScript 中为我们的服务使用 TestBed?如果是这样,你能提供一个如何做到这一点的例子吗?
将服务作为第二个参数传递给 nsTestBedBeforeEach
,然后使用 TestBed
获取实例
import { TestBed } from "@angular/core/testing";
import { nsTestBedBeforeEach } from "nativescript-angular/testing";
import { ItemService } from "../app/item/item.service";
describe("ItemsService Test", () => {
let service: ItemService;
beforeEach(nsTestBedBeforeEach([], [ItemService]));
it("should use ItemService", () => {
service = TestBed.get(ItemService);
expect(service.getItems).toBeTruthy();
expect(service.getItem).toBeTruthy();
});
});
var mainViewModel = require("../main-view-model"); //Require the main view model to expose the functionality inside it.
describe("Hello World Sample Test:", function() {
it("Check counter.", function() {
expect(mainViewModel.createViewModel().counter).toEqual(42); //Check if the counter equals 42.
});
it("Check message.", function () {
expect(mainViewModel.createViewModel().message).toBe("42 taps left"); //Check if the message is "42 taps left".
});
});
我正在使用 NativeScript 6+ 和 Angular 8+ 编写应用程序。 我正在尝试编写一些单元测试并启动它们 运行。 我已阅读有关单元测试的文档:https://docs.nativescript.org/tooling/testing/testing 我进行了一些单元测试和 运行 普通服务。 例如
import { ItemService } from '../app/item/item.service';
describe('ItemsService Test', () => {
let service: ItemService;
beforeEach(() => { service = new ItemService(); });
it('should be defined',() => {
expect(service).toBeTruthy();
});
it('should have its functions defined',() => {
expect(service.getItems).toBeTruthy();
expect(service.getItem).toBeTruthy();
});
});
我看到我们可以通过在构造函数和模拟服务中实例化它们来测试具有依赖性的服务。 https://angular.io/guide/testing#service-tests
是否可以在 NativeScript 中为我们的服务使用 TestBed?如果是这样,你能提供一个如何做到这一点的例子吗?
将服务作为第二个参数传递给 nsTestBedBeforeEach
,然后使用 TestBed
import { TestBed } from "@angular/core/testing";
import { nsTestBedBeforeEach } from "nativescript-angular/testing";
import { ItemService } from "../app/item/item.service";
describe("ItemsService Test", () => {
let service: ItemService;
beforeEach(nsTestBedBeforeEach([], [ItemService]));
it("should use ItemService", () => {
service = TestBed.get(ItemService);
expect(service.getItems).toBeTruthy();
expect(service.getItem).toBeTruthy();
});
});
var mainViewModel = require("../main-view-model"); //Require the main view model to expose the functionality inside it.
describe("Hello World Sample Test:", function() {
it("Check counter.", function() {
expect(mainViewModel.createViewModel().counter).toEqual(42); //Check if the counter equals 42.
});
it("Check message.", function () {
expect(mainViewModel.createViewModel().message).toBe("42 taps left"); //Check if the message is "42 taps left".
});
});