I would like to see suggestions if my code could be more improved. In my controller I have a line:

self.bool_value = False  # for testing

which in my opinion should not be there, because I put it there only for testing purposes, but I did not find any way around. In addition I would like to know whether you guys test the __init__ method as well, because I see no point in it. And another question would be, if you guys would test if the parameter req was called in the execute method. Any other suggestions to improve this code are welcomed.

My Controller looks like this:

from typing import Callable
from source.controller.RequestModel import RequestModel
from source.boundaries.AbstractInputBoundary import       AbstractInputBoundary

class Controller:

abstractInputBoundary: AbstractInputBoundary
bool_function: Callable[[bool], bool]
bool_value: bool

def __init__(self, abstract_input_boundary, bool_function=lambda _:     True):
    self.abstractInputBoundary = abstract_input_boundary
    self.bool_function = bool_function
    self.bool_value = True

def process_input(self) -> None:
    Waits for the input of the User in order to call a usecase
    :rtype: None
    while self.bool_function(self.bool_value):
        self.bool_value = False  # for testing
        inp: str = input()

        if inp == "show data":
            req = RequestModel()

My Test looks like the following:

import unittest
from source.controller.Controller import Controller
from unittest.mock import patch
from unittest.mock import MagicMock

class TestController(unittest.TestCase):

def test_process_input(self):
    This tests, whether after the input "show data" in the controller object a boundary object is called

    def switch_function(switch_bool):
        if switch_bool is True:
            return True
            return False

    with patch('builtins.input') as inp:
        inp.return_value = "show data"
        boundary = MagicMock()
        boundary.execute = MagicMock()
        controller = Controller(boundary, switch_function)
        assert boundary.execute.called

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.