question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

new bug introduced/functionality removed in upgrade from 1.3 to 1.4

See original GitHub issue

I just upgraded from mockito-1.3.0 to mockito-1.4.0 and many of my tests began to fail. Here is “somewhat minimal” example of what kind of code used to work, but it doesn’t anymore (python 3.7.3):

from typing import Callable
from nose.tools import assert_equal
from mockito import mock, when


class _c(object):
    def __init__(self, fn: Callable[[int], str]) -> None:
        self._fn = fn

    def get_val(self, a: int) -> str:
        return self._fn(a)


def test_foo():
    x = mock()
    c = _c(x.get_value)
    when(x).get_value(10).thenReturn('foo')
    assert_equal(c.get_val(10), 'foo')

result:

$ nosetests a.py 
F
======================================================================
FAIL: a.test_foo
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/spurny/py-venv/wf3/lib/python3.7/site-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/spurny/src-experiments/mockito-1.4-bug/a.py", line 18, in test_foo
    assert_equal(c.get_val(10), 'foo')
AssertionError: None != 'foo'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)

but if I mock the x.get_value before creating c:

...
def test_foo():
    x = mock()
    when(x).get_value(10).thenReturn('foo')  # moved here from below `c = _c(...` line
    c = _c(x.get_value)
    assert_equal(c.get_val(10), 'foo')

then it works fine:

$ nosetests a.py 
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kastecommented, Oct 14, 2022

You mean a different way from your original example? Typically just pass in a mock(). Then early or late configuration works:

class _c(object):
    def __init__(self, fn):
        self._fn = fn

    def get_val(self, a):
        return self._fn(a)


def test_foo():
    x = mock()
    c = _c(x)
    when(x).__call__(10).thenReturn('foo')
    assert c.get_val(10) == 'foo'

In that case you can also turn on “strictness” so it has a better error message.

image

(Otherwise you always have the None is not ‘foo’ assertion error.)

0reactions
jan-spurnycommented, Oct 14, 2022

Perfect, thanks! I didn’t thought of mocking __call__

Read more comments on GitHub >

github_iconTop Results From Across the Web

Azure AD Connect: Upgrade from a previous version to the latest
Explains the different methods to upgrade to the latest release of Azure Active Directory Connect, including an in-place upgrade and a swing ...
Read more >
What's new in 1.4.0 (January 22, 2022) - Pandas
The new function supports the method , ascending , and pct flags of ... introduced in Python 3.9 to remove pre-/suffixes from string-type...
Read more >
WinBUGS - MRC Biostatistics Unit - University of Cambridge
Contents. Introduction to WinBUGS; Installing WinBUGS 1.4 in Windows. Obtaining the key for unrestricted use; Upgrading to version 1.4.3. Problems ...
Read more >
What's New in SQLAlchemy 1.4?
ORM Query is internally unified with select, update, delete; 2.0 style execution ... this feature was first introduced in version 1.3 and is...
Read more >
Changelog — JupyterHub 3.1.0 documentation
0 is a small release, fixing various bugs and introducing some small new features and metrics. See more details below. Thanks to many...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found