I am new in mock and tests for python code(whatever code).
I'm trying to test my function main.py
def get_channel_list():
sc = SlackClient(get_token())
channels_list_json = sc.api_call("channels.list")
if channels_list_json['ok'] == True:
return channels_list_json
that is function that I'm trying to test
I need to mock patch sc.api_call("channels.list") to return JSON object
but I can't find any examples like this that would help me to figure out how to do it.
Evrething I found was like this example Mocking a class method...
I think it would look like this:
@patch.object(SlackClient, 'api_call')
def test_get_channel_list():
assert get_channel_list() != ""
I don't have to to test lib I need to test the rest of my code in the function that I mentioned before. Thanks for any help, I'm realy stack with this test.
You need to write a separate mock function to return a JSON object.
You can try this:
@pytest.fixture
def mock_api_call(monkeypatch):
monkeypatch.setattr(SlackClient, 'api_call', lambda self, arg: {"ok": True})
def test(mock_api_call):
sc = SlackClient(get_token())
channels_list_json = sc.api_call("channels.list")
assert True == channels_list_json['ok']
def test_get_channel_list(mock_api_call):
channels_list_json = get_channels_list()
assert dict == type(channels_list_json)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With