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.

How to mock the class properties for unit tests

See original GitHub issue

Hi,

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:closed
  • Created 5 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
aislanmaiacommented, Nov 19, 2019

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:

image

1reaction
zhango90commented, Jul 8, 2020

@farzadmf I solved the problem using the spy method in jest: const spy = jest.spyOn(<vuexModule> , '<actionName>');

I hope it helps.

Read more comments on GitHub >

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

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