0

I'm trying to mock the object, But it's not happening can anyone help me

payment_gateway

----payment.py

class Razz:
    def __init__(self) -> None:
        self.base_url = "https://api.example.com/v1"
        self.api_key = os.environ["API_KEY"]
        self.api_secret = os.environ["SECRET"]
        self.kh_account_number = os.environ["ACCOUNT_NUMBER"]

    def create_contact(self):
       res = request.post(usrl=f"{base_url}/contact", data=payload, auth(self.api_key,self.api_secret)
       return "id"

And am importing this class on another fille i.e event_bank_deails.py

from payment_gateway.payment import Razz, PaymentGatewayErrors

PAYMENT_GATEWAY= Razz()

def update_bank_detials(request: Request) -> Response:
    contact_id = PAYMENT_GATEWAY.create_contact(body, event_id)  # Creating contact         
    fund_id = PAYMENT_GATEWAY.create_account(contact_id, body)  # Creating fund account on razorpayX
    return resposes

TestCases file

@patch("event_bank_details.event_bank_details.Razz")
@pytest.mark.parametrize("input, expected_op", UPDATE_EVENT_BANK_DETAILS_INPUT)
    def test_update_event_bank_details(mock_email, mock_object, input, expected_op):
        from event_bank_details.event_bank_details import update_bank_detials
        # mock_object.return_value.create_contact.return_value = None
        # mock_object.return_value.create_account.create_account = None
        response = update_bank_detials(input)
        response.prepare()
        response_dict = response.get()
        assert response_dict["statusCode"] == expected_op

And then am writing test cases for the update_bank_details function It's throwing an error invalid API_KEYandAPI_SECREATEHow can I mock theRazzclassinit()` call on update_bank_details function???

Uday
  • 1
  • 1
  • 1
    The code you shared is not usable for anybody to provide help. Can you update with [minimal-reproducible-example](https://stackoverflow.com/help/minimal-reproducible-example) Moreover look into https://docs.pytest.org/en/6.2.x/monkeypatch.html as well. – teedak8s Jun 17 '22 at 03:17
  • Additionally, code snippets are for JavaScript/HTML/CSS not for Python. You probably want to format that code as a code sample. – Hernán Alarcón Jun 17 '22 at 03:23
  • @AkhilJain, can you check now??? – Uday Jun 17 '22 at 03:30
  • The core of your issue you have is that you're patching the class, `Razz`, *after* the `PAYMENT_GATEWAY` instance of it has already been created at top level in your second module. I'm afraid my mocking skills are not up to the task of showing you the right way to do this, but one idea that might help is changing when you create the instance of `Razz`. – Blckknght Jun 17 '22 at 03:50
  • Instead of trying to patch `Razz` you could try to patch `os.environ`. Check [this answer](https://stackoverflow.com/a/31583528/2738151). – Hernán Alarcón Jun 17 '22 at 03:53

0 Answers0