简体   繁体   中英

How can mock.assert_called_once() be told to accept any instance of an object?

I have some code which is calling Mock assert_called_one_with() .

  import mock
  with mock.patch.object(myfile.Sheet,  'WriteData', autospec=True) as mock_sheets:
    somefile.ExportToSheets(query, 'api_key', 'sheet_id', 'A1')
    mock_sheets.assert_called_once_with(myfile.Sheet(), 'A1', [('a', 'b'), (1, 2)])

As you can see, everything compares equally except for the object instance.

AssertionError: Expected call: \
WriteData(<myfile.Sheet object at 0x7f9d487923c8>, 'A1', [('a', 'b'), ('1', '2')])
Actual call: \
WriteData(<myfile.Sheet object at 0x7f9d47bafb38>, 'A1', [('a', 'b'), ('1', '2')])

How can I either

  • tell assert_called_once_with() to ignore object identity, or
  • perform a similar assertion?

mock.ANY will match any value

mock_sheets.assert_called_once_with(mock.ANY, 'A1', [('a', 'b'), (1, 2)])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM