I have a component that contains statement like this.$route.fullPath
, how should I mock value of fullPath
of $route
object if I want to test that component?
相关问题
- Dependencies while implementing Mocking in Junit4
- How to unit test a reactive component where ngCont
- Simple vue app doesn't show anything
- Getting Uncaught TypeError: …default is not a cons
- Vue.js computed property loses its reactivity when
相关文章
- How to replace file-access references for a module
- How to mock methods return object with deleted cop
- What is a good way of cleaning up after a unit tes
-
EF6 DbSet
returns null in Moq - React testing library: Test attribute / prop
- React/JestJS/Enzyme: How to test for ref function?
- vue-router how to persist navbar?
- python unit testing methods inside of classes
All kudos to @SColvin for his answer; helped find an answer in my scenario wherein I had a component with a router-link that was throwing a
during unit test because Vue hadn't been supplied with a router. Using @SColvin answer to rewrite the test originally supplied by vue-cli from
to
Not needing to pass parameters in to the view I could simplify the component as the default render, no need to push and no need to wait nextTick. HTH someone else!
I disagree with the top answer - you can mock
$route
without any issue.On the other hand, installing vue-router multiple times on the base constructor will cause you problems. It adds
$route
and$router
as read only properties. Which makes it impossible to overwrite them in future tests.There are two ways to achieve this with vue-test-utils.
Mocking vue-router with the mocks option
You can also install Vue Router safely by using createLocalVue:
Installing vue-router safely in tests with createLocalVue
Best not mock
vue-router
but rather use it to render the component, that way you get a proper working router. Example:Things to note:
render: h => h('router-view')
.totest
component, but others might be required if they're referenced bytotest
eg.another_component
in this example.nextTick
for the HTML to have rendered before you can look at it/test it.One of the problems is that most of the examples I found referred to the old version of
vue-router
, see the migrations docs, eg. some examples userouter.go()
which now doesn't work.Easiest way i've found is to mock the $route.
Adding to the great answer from @SColvin, here's an example of this working using Avoriaz:
I believe this should work with vue-test-utils, too.
Take a look at this example using vue-test-utils, where I'm mocking both router and store.