0

I have a class that looks like that:

class StoryManager:
    STORIES = {1:'stories/itsmyluck.txt', 2:'stories/triptothezoo.txt', 3:'stories/loveatfirstsight.txt'}

    def get_fields(self, directory):
        self.pattern = re.compile(r'<\s*(.*?)\s*>')
        self.results = []
        with open(directory, 'r', encoding='utf-8') as f:
            text = f.read()
            self.matches = self.pattern.finditer(text)

            for self.match in self.matches:
                self.results.append(self.match[1])
        return self.results

and another file that I write tests in:

from app import StoryManager
import unittest

class TestStoryManager(unittest.TestCase):

    def test_get_fields(self):
        result = ['case1','case2','case3','case4','case5','case6',
                  'case7','case8','case9','case10','case11','case12']

        dirs = ['tests/story1.txt', 'tests/story2.txt', 'tests/story3.txt']

        self.assertEqual(StoryManager.get_fields(dirs[0]), result)

if __name__ == '__main__':
    unittest.main()

and the problem is that python says :

Traceback (most recent call last):
  File "d:\Programowanie ;3\GitRepository\madlibs\test_storymanager.py", line 12, in test_get_fields
    self.assertEqual(StoryManager.get_fields(dirs[0]), result)
TypeError: get_fields() missing 1 required positional argument: 'directory'

but as I can see there is argument(that is dirs[0]).

I feel like it is something simple but I can't find out what's wrong. Thanks.

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
viGor027
  • 67
  • 6
  • 1
    Look more closely at `StoryManager.get_fields(dirs[0]), result` – Countour-Integral Feb 09 '21 at 20:18
  • 1
    `StoryManager.get_fields(dirs[0])`: you're trying to access a class method. `def get_fields(self, directory):`: You've defined an instance method – Pranav Hosangadi Feb 09 '21 at 20:19
  • 1
    You need an Instance of your class to access `.get_fields(..)` - `sm = StoryManager(); self.assertEqual(sm.get_fields(dirs[0]), result)` – Patrick Artner Feb 09 '21 at 20:19
  • @PatrickArtner I added `test_object = StoryManager()` above my method inside body of the class, but now it says that `NameError: name 'test_object' is not defined` of course I replaced `StoryManager.get_fields(dirs[0]), result)` with `test_object.get_fields(dirs[0]), result)` – viGor027 Feb 09 '21 at 20:30
  • Why above the method. put it inside. [short-description-of-the-scoping-rules](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) if you make it a class-object you need to use `StoryManager.test_object` - go through the scoping rules – Patrick Artner Feb 09 '21 at 20:36
  • That helped, thanks <3 – viGor027 Feb 09 '21 at 20:44

0 Answers0