mock a class in python that used by other class

60 Views Asked by At

I am trying to mock a function on a class that calls a function that is in other calls a method of other class as shown below.

class check:
    def __init__(self):
        asdad

    def b(self):
        return class_test.MyClass.a()

class MyClass():
    def a():
        return "qwertu"

I tried to mock a function "b" of class "check" using Mock from "unittest.mock" and I am able to mock it as shown below

def test_mock_class():
    main.check.b = Mock(return_value='mocked value')
    assert main.check.b() == 'mocked value'

and it is working as expected.

now i am trying to mock the method of extended class as shown below

def test_mock_class():
    class_test.MyClass.a = Mock(return_value='mocked value')
    ob = main.check()
    assert ob.b() == 'mocked value'

but it is not working as expected, it says that "qwertu" is not eqaul to "mocked value", but i have already mocked a() method of MyClass, what is the problem.

0

There are 0 best solutions below