angular - 在其中一个测试中为 Testbed 提供者添加一个值
问题描述
如何在测试中更改 Testbed 提供者的值,而无需重新定义整个 TestBed?
describe('a test', () => {
let router: Router;
let route: ActivatedRoute;
let location: Location;
// want to be able to update this object
let initialQueryParams: {} = {};
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([
{
path: '',
component: DummyComponent,
}
])
],
providers: [{
provide: ActivatedRoute,
useValue: {
snapshot: {
queryParams: initialQueryParams
}
}
}],
router = TestBed.inject(Router);
route = TestBed.inject(ActivatedRoute);
location = TestBed.inject(Location);
});
it('should...', () => {
functionWhichHasRouterNavigate();
expect(location.path()).toBe('/');
});
it('should do something else...', () => {
initialQueryParams = { baz: 'qux' };
functionWhichHasRouterNavigate();
expect(location.path()).toBe('/?baz=qux');
});
});
我尝试将 beforeEach 块中的所有内容都包装成一个 function setup = () => { ... }
,但这使得测试变得脆弱。价值观以某种方式相互泄漏
解决方案
推荐阅读
- c# - getter 中的自定义查询以在缺少组合键的一部分时返回特定值
- azure - 如何使用 java sdk 基于自定义 VM 映像创建 azure 批处理池
- android - 有没有办法在应用程序启动时隐藏片段或 ID?
- javascript - AMP如何从带有ref的链接中动态获取href
- docker - 如何在 docker compose 版本中启用 IPv6:> = 3?
- java - JAVA_HOME 未使用 Maven 包装器正确定义 Spring Boot 项目
- docker - 我们如何在 docker history 命令的输出中看到完整的命令?
- python - Offset pandas dates step by step
- gzip - 如何有条件地在 Caddy 中应用 gzip 指令?
- vba - 如何使用 Word VBA 更改从文档的第一段生成的文件名的大小写