How to mock the class properties for unit tests
See original GitHub issueHi,
I have my module something like this:
export interface IUsersModule {
users: User[];
}
@Module({
dynamic: true,
name: 'users',
namespaced: true,
store,
})
class UsersModule extends VuexModule implements IUsersModule {
users: Users[] = [];
// ... actions, mutations, etc.
}
export const usersModule = getModule(UsersModule);
And I have my component like this:
<template>
<div>
<p v-for="user in users">{{ user.name }}</p>
</div>
</template>
<script lang="ts">
import { usersModule } from './usersModule';
@Component
export default class MyComponent extends Vue {
private get users() {
return usersModule.users;
}
}
</script>
Now, I want to unit test MyComponent
. Is there a way to use jest
to mock the users
property? I tried to do something like this:
import { usersModule } from './usersModule';
const usersMock = jest.SpyOn(usersModule, 'users');
But, when I do this, I get the error that says:
Cannot spy the users property because it is not a function; object given instead
Can you please give some guidance on this?
Thank you in advance
Issue Analytics
- State:
- Created 5 years ago
- Comments:7
Top Results From Across the Web
Better way to mock class attribute in python unit test
from mock import patch from unittest import TestCase from base import Base class MyTest(TestCase): @patch('base.
Read more >Properties - Unit Testing in C#
In case a property of the mock should behave like an automatic property, developers can instruct Moq to track the values passed to...
Read more >unittest.mock — mock object library — Python 3.11.1 ...
unittest.mock is a library for testing in Python. It allows you to replace parts of your system under test with mock objects and...
Read more >Mock Properties | JustMock Documentation - Telerik
The set operation can be mocked for both indexers and normal properties. Set operations are mocked using a special set entry point which...
Read more >Unit Testing in C# With Moq - Wake up the Testing Genius ...
While mocking a class is not recommended, it is possible to do so. To make a class mockable, it needs to be a...
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 FreeTop 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
Top GitHub Comments
I’m receiving this
module namespace not found...
And the docs of this lib doesn’t even take a simple note about tests. I’m lost in the jungle trying to test things like this:@farzadmf I solved the problem using the spy method in jest:
const spy = jest.spyOn(<vuexModule> , '<actionName>');
I hope it helps.