0

I have a project assigning 2 configs in my main __init__ file since they are used frequently throughout the project.

#__init__.py

from config import Config

config1 = Config('Email')
config2 = Config('Test')

My Config class within config.py has a method called content that I need to mock out on instances config1 and config2. The config does call out to a third party library to do an http request, so I need to return a json dictionary for the response to content.

In a validators function I have the following:

#validation.py

from parser import config1, config2

def validation(msg):
    if "email" in config1.keys():
        ...

I'm not trying to mock out the tests but keep getting errors. I've tried various mock patch paths but none work.

My latest attempt is the following:

from mock import patch
from parser import validation

@patch('parser.Config')
def test_is_valid(mock_config):

    mock_config.return_value.content = "Test"

    assert validation.is_valid("email") == True

What am I doing wrong that my instances of Config (config1, and config2) are not correctly returning the .content values? Thanks

staten12
  • 735
  • 3
  • 9
  • 20
  • Is there a specific error or unexpected result that you're getting? Have you tried changing `@patch('parser'.Config')` to `@patch('parser.config1')`? – Andrew Seaman May 11 '18 at 18:38
  • Yes the 'parser.config1' does not work 95% of the patches I try give HTTPS unauthenticated errors since I'm trying to mock out the http request that gets triggered from config1.content – staten12 May 11 '18 at 19:36
  • `mock_config.return_value.content = "Test"` is wrong, it should be patched as `@mock.patch('package.config.Config.content')` and `mock_content.return_value = {}`, the http request and response should be mocked as well, but without code of Config class, it shot in the dark – Gang May 11 '18 at 22:27

0 Answers0