在Angular中,可以使用Angular的测试工具和模拟对象来模拟路由状态。为了在单元测试中模拟路由状态,我们可以使用Angular的RouterTestingModule和Router类的实例来创建一个模拟的路由状态。 首先,我们需要导入RouterTestingModule和Router类: 代码语言:txt 复制 import { RouterTestingModule }
我们使用RouterTestingModule来模拟路由功能。 在测试文件中,使用TestBed.configureTestingModule方法配置测试模块。在这个方法中,我们导入RouterTestingModule并将TestComponent声明为测试组件。 在每个测试用例之前,使用TestBed.createComponent方法创建组件的实例,并通过fixture.componentInstance获取组件实例的引用。 在测试用例中,...
2、RouterTestingModule 当需要进行路由导航势必需要与location打交道,得需要获取 URL 信息来验证导航的结果,是吧! 当然,不必想太多,因为Angular工具集帮我们做了一个很常用的 Spy Module,以便写测试代码,名也:RouterTestingModule。 beforeEach(async(() => { TestBed.configureTestingModule({ imports: [AppModule, ...
import { TestBed, async } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { AppComponent } from './app.component'; describe('AppComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ RouterTestingModul...
TestBed.configureTestingModule({ imports: [ RouterTestingModule ], declarations: [ AppComponent ], }).compileComponents(); })); it('should create the app', async(() => { const fixture = TestBed.createComponent(AppComponent); const app = fixture.debugElement.componentInstance; ...
Previously, RouterTestingModule only assigned two of the options within ExtraOptions to the router. Now, it assigns the same options as RouterModule does (with the exception of enableTracing at the request of @atscott) via a new shared function assignExt
TestBed.configureTestingModule()方法动态构建TestingModule来模拟Angular @NgModule,支持@NgModule的大多数属性。 测试中需导入测试的组件及依赖。在AppComponent页面中使用了router-outlet,因此我们导入了RouterTestingModule来模拟RouterModule。Test Module预配置了一些元素,比如BrowserModule,不需导入。 TestBed.createComponent...
beforeEach(()=>{TestBed.resetTestEnvironment();...TestBed.configureTestingModule({declarations:[TestComp],teardown:{destroyAfterEach:true}});}); 这样可以使每个项目灵活的根据场景和需要选择适合的方式,可以查看Lars Gyrup Brink Nielsen的博客学习更多。
- The behavior of the `SpyLocation` used by the `RouterTestingModule` has changed to match the behavior of browsers. It no longer emits a 'popstate' event when `Location.go` is called. In addition, `simulateHashChange` now triggers _both_ a `hashchange` and a `popstate` event. ...
TestBed.configureTestingModule({imports:[RouterTestingModule],declarations:[AppComponent],}).compileComponents(); TypeScript to add the dependencies for the component being tested. We mount the component being tested with: constfixture=TestBed.createComponent(AppComponent); ...