Skip to main content

How to mock a function inside a class method

I am trying to mock a method of an instance that is being used and impeded into another class. This is in order to test the class independent of that method.

This is the class and the function that needs to be mocked is redis_client.fields_allowed:

#app.auth.roles.py
from app.redis.client import redis_client

role_count = 0


@dataclass
class Role:
    id: int
    name: str
    byte: int

    @staticmethod
    def new(name: str, *roles: Role) -> Role:
        global role_count
        role_count += 1
        byte: int = 1 << role_count
        for role in roles:
            byte |= role.byte
        return Role(id=role_count, name=name, byte=byte)

    def __or__(self, other: Union[Role, int]) -> int:
        if isinstance(other, Role):
            return self.byte | other.byte
        return self.byte | other

    def __and__(self, other: Union[Role, int]) -> int:
        if isinstance(other, Role):
            return self.byte & other.byte
        return self.byte & other

    def fields(self, *field_ids: int) -> Callable[[int], Role]:
        def func(user_id: int) -> Role:
        #to be mocked
            if redis_client.fields_allowed(user_id, self.id, list(field_ids)): 
                return self
            return Role(id=0, name="None", byte=0)

        return func

This is my test (access_token fixture created before):

#app.auth.test_auth.py
@patch.object(roles.Role, "fields.func.redis_client.fields_allowed")
def test_field_ownership(access_token, fields_allowed):
    fields_allowed.return_value = True
    access_token_str = access_token
    #etc

The test gives fixture 'fields_allowed' not found. How can I mock it? without rewriting the class and using dependency injection with object redis_client and class Role.

Update.

It looks like the patched function needs to come before the fixture:

@patch("app.auth.roles.redis_client.fields_allowed")
def test_valid_field_ownership(fields_allowed, access_token):
    fields_allowed.return_value = True
    access_token_str = access_token

Now it looks like it is patched. However it does not seem to return True when called. see below

#method in Role class 
#...
    def fields(self, *field_ids: int) -> Callable[[int], Role]:
        def func(user_id: int) -> Role:
            if v := redis_client.fields_allowed(user_id, self.id, list(field_ids)):
                print(v) #prints <coroutine object AsyncMockMixin._execute_mock_call at 0x7f8cb7e5e340>
                return self
            else:
                raise error.NotEnoughClaims(
                    status_code=401, message="Not enough claims"
                )

        return func

The return looks like an instance of the patch <coroutine object AsyncMockMixin._execute_mock_call at 0x7f8cb7e5e340> instead of True. Why is this happening?

Update.

I found the issue. redis_client.fields_allowed is awaitable. Therefore I needed to make some fns async and also I needed to use pytest-asyncio plugin for the tests to use async fns.



source https://stackoverflow.com/questions/74363748/how-to-mock-a-function-inside-a-class-method

Comments

Popular posts from this blog

Where and how is this Laravel kernel constructor called? [closed]

Where and how is this Laravel kernel constructor called? public fucntion __construct(Application $app, $Router $roouter) { } I have read the documentation and some online tutorial but I can find any clear explanation. I am learning Laravel and I am wondering where does this kernel constructor receives its arguments from. "POSTMOTERM" CLARIFICATION: Here is more clarity.I have checked the boostrap/app.php and it is only used for boostrapping the interfaces into the container class. What is not clear to me is where and how the Kernel class is instatiated and the arguments passed to the object calling the constructor.Something similar to; obj = new kernel(arg1,arg2) or, is the framework using some magic functions somewhere? Special gratitude to those who burn their eyeballs and brain cells on this trivia before it goes into a full blown menopause alias "MARKED AS DUPLICATE". To some of the itchy-finger keyboard warriors, a.k.a The mods,because I believe in th...

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype