question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RouterService.urlFor causes error in non-application tests.

See original GitHub issue

Steps to reproduce

  1. Use urlFor() in a component and output the value in the template
  2. 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:closed
  • Created 5 years ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
rwjbluecommented, Dec 10, 2018

Yes, I totally agree that this is a bug. During setupRenderingTests type tests, I would absolutely want this.router.urlFor(...) and other methods to function properly.

The issue isn’t really with autoboot=false though, inside setupTest / setupRenderingTest type 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 do this.owner.setupRouter() in your test setup.

I’d gladly accept a PR to call owner.setupRouter() from any method that inherently requires the underlying router:main in the router service.

1reaction
pixelhandlercommented, Jul 31, 2018

@apellerano-pw Thanks for the explanation 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found