我正在使用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();
});
});
我看到我们可以通过在构造函数中实例化依赖项并通过模拟服务来测试Services。 https://angular.io/guide/testing#service-tests
是否可以在NativeScript中将TestBed用于我们的服务?如果是这样,您能提供一个示例吗?
答案 0 :(得分:1)
将服务作为第二个参数传递给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();
});
});
答案 1 :(得分:0)
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".
});
});