RouterService.urlFor causes error in non-application tests.
See original GitHub issueSteps to reproduce
- Use
urlFor()in a component and output the value in the template - Attempt to render the component in an integration/rendering test
TypeError: Cannot read property 'generate' of undefined
at Class.generate (https://cdn.jsdelivr.net/npm/ember-source@3.2.2/dist/ember.debug.js:33524:59)
at Class.urlFor (https://cdn.jsdelivr.net/npm/ember-source@3.2.2/dist/ember.debug.js:31371:48)
Expected behavior
The test doesn’t crash.
Since autoboot=false is in use it makes sense that there’s no Router for RouterService to work with. However, I would expect it to return empty data rather than crash. Similar to how {{link-to}} behaves in the same scenario.
Twiddle
If you click Run Tests you’ll see the exception. https://ember-twiddle.com/bc59e11d525fc3548519f66250173a32?openFiles=my-component.component.js%2C
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (6 by maintainers)
Top Results From Across the Web
RouterService - 4.8 - Ember API Documentation
urlFor (routeName, models, options) String public the model(s) for the route. Generate a URL based on the supplied route name and optionally a...
Read more >Ember.js: CHANGELOG.md | Fossies
urlFor(...) and routerService.recognize(...) does not error if the router is not fully ... #19080 [BUGFIX] Lazily setup the router in non-application tests ......
Read more >ember-source | Yarn - Package Manager
urlFor(...) and routerService.recognize(...) does not error if the router is not fully ... #19080 [BUGFIX] Lazily setup the router in non-application tests ......
Read more >No provider for router" while writing Karma-Jasmine unit test ...
You need to import RouterTestingModule when setting up the test module. /* tslint:disable:no-unused-variable */ import { async, ...
Read more >ember-source: Versions - Openbase
urlFor (...) and routerService.recognize(...) does not error if the router is not ... [BUGFIX] Lazily setup the router in non-application tests for component ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Yes, I totally agree that this is a bug. During
setupRenderingTeststype tests, I would absolutely wantthis.router.urlFor(...)and other methods to function properly.The issue isn’t really with
autoboot=falsethough, insidesetupTest/setupRenderingTesttype tests you do have a fully booted application, but we specifically avoid starting routing as part of that setup. We probably should make the router service a tad more resilient in these cases. As a temporary work around @apellerano-pw you could dothis.owner.setupRouter()in your test setup.I’d gladly accept a PR to call
owner.setupRouter()from any method that inherently requires the underlyingrouter:mainin the router service.@apellerano-pw Thanks for the explanation 😃