我正在使用NativeScript 6+和Angular 8+编写应用程序。 我正在尝试编写一些单元测试并使它们启动并运行。 我已阅读有关单元测试的文档:https://docs.nativescript.org/tooling/testing/testing 我运行了 tns test init 和 tns test android 来准备测试。 我按照那里的指示来设置测试和使用TestBed。我的测试无法正常工作并引发错误。 这是我的存储库:https://github.com/aubrey-fowler/NativeScriptUnitTests
样本测试:
using System;
using Moq;
using MugenMvvmToolkit;
using MugenMvvmToolkit.Infrastructure;
using MugenMvvmToolkit.Interfaces;
using MugenMvvmToolkit.Interfaces.Presenters;
using MugenMvvmToolkit.Models;
using Xunit;
namespace Mobile.Pull.Agro.Tests
{
public class UnitTest1 : UnitTestBase
{
public UnitTest1()
{
_viewModelPresenterMock = new Mock<IViewModelPresenter>();
_serializer = new Serializer(AppDomain.CurrentDomain.GetAssemblies());
var container = new AutofacContainer();
container.BindToConstant(_viewModelPresenterMock.Object);
Initialize(container, new DefaultUnitTestModule());
ApplicationSettings.CommandExecutionMode = CommandExecutionMode.None;
}
private Mock<IViewModelPresenter> _viewModelPresenterMock;
private ISerializer _serializer;
[Fact]
public void Test1()
{
Assert.True(true);
}
}
}
运行测试时,出现此错误:
import { ItemsComponent } from '../app/item/items.component';
import { ItemService } from '../app/item/item.service';
import { nsTestBedAfterEach, nsTestBedBeforeEach, nsTestBedRender } from 'nativescript-angular/testing';
describe('ItemsComponent Test', () => {
beforeEach(nsTestBedBeforeEach([ItemService, ItemsComponent]));
afterEach(nsTestBedAfterEach(false));
it('should be defined', () => {
nsTestBedRender(ItemsComponent).then((fixture) => {
fixture.detectChanges();
const component = fixture.componentInstance;
expect(component).toBeTruthy();
});
});
});