import unittest
from event import Event


class EventTest(unittest.TestCase):
    def test_a_listener_is_notified_when_event_is_raised(self):
        called = False

        def listener():
            nonlocal called
            called = True

        event = Event()
        event.connect(listener)
        event.fire()
        self.assertTrue(called)

    def test_a_listener_is_passed_correct_parameters(self):
        params = ()

        def listener(*args, **kwargs):
            nonlocal params
            params = (args, kwargs)

        event = Event()
        event.connect(listener)
        event.fire(5, shape="square")
        self.assertEquals(((5, ), {"shape": "square"}), params)

import unittest
from event import Event


class Mock:
    def __init__(self):
        self.called = False
        self.params = ()

    def __call__(self, *args, **kwargs):
        self.called = True
        self.params = (args, kwargs)


class EventTest(unittest.TestCase):
    def test_a_listener_is_notified_when_event_is_raised(self):
        listener = Mock()
        event = Event()
        event.connect(listener)
        event.fire()
        self.assertTrue(listener.called)

    def test_a_listener_is_passed_correct_parameters(self):
        listener = Mock()
        event = Event()
        event.connect(listener)
        event.fire(5, shape="square")
        self.assertEquals(((5, ), {"shape": "square"}), listener.params)

Refactor a lightweight Mock class.
Refactor a lightweight Mock class.

  import unittest
  from event import Event
  
  
+ class Mock:
+     def __init__(self):
+         self.called = False
+         self.params = ()
+ 
+     def __call__(self, *args, **kwargs):
+         self.called = True
+         self.params = (args, kwargs)
+ 
+ 
  class EventTest(unittest.TestCase):
      def test_a_listener_is_notified_when_event_is_raised(self):
-         called = False
- 
-         def listener():
+         listener = Mock()
-             nonlocal called
-             called = True
- 
          event = Event()
          event.connect(listener)
          event.fire()
-         self.assertTrue(called)
+         self.assertTrue(listener.called)
  
      def test_a_listener_is_passed_correct_parameters(self):
+         listener = Mock()
-         params = ()
- 
-         def listener(*args, **kwargs):
-             nonlocal params
-             params = (args, kwargs)
- 
          event = Event()
          event.connect(listener)
          event.fire(5, shape="square")
-         self.assertEquals(((5, ), {"shape": "square"}), params)
+         self.assertEquals(((5, ), {"shape": "square"}), listener.params)
  
Refactor a lightweight Mock class.
## Code Before:
import unittest
from event import Event


class EventTest(unittest.TestCase):
    def test_a_listener_is_notified_when_event_is_raised(self):
        called = False

        def listener():
            nonlocal called
            called = True

        event = Event()
        event.connect(listener)
        event.fire()
        self.assertTrue(called)

    def test_a_listener_is_passed_correct_parameters(self):
        params = ()

        def listener(*args, **kwargs):
            nonlocal params
            params = (args, kwargs)

        event = Event()
        event.connect(listener)
        event.fire(5, shape="square")
        self.assertEquals(((5, ), {"shape": "square"}), params)

## Instruction:
Refactor a lightweight Mock class.
## Code After:
import unittest
from event import Event


class Mock:
    def __init__(self):
        self.called = False
        self.params = ()

    def __call__(self, *args, **kwargs):
        self.called = True
        self.params = (args, kwargs)


class EventTest(unittest.TestCase):
    def test_a_listener_is_notified_when_event_is_raised(self):
        listener = Mock()
        event = Event()
        event.connect(listener)
        event.fire()
        self.assertTrue(listener.called)

    def test_a_listener_is_passed_correct_parameters(self):
        listener = Mock()
        event = Event()
        event.connect(listener)
        event.fire(5, shape="square")
        self.assertEquals(((5, ), {"shape": "square"}), listener.params)
