user3325789
user3325789

Reputation: 413

How to test a class with a very complex constructor?

I have inherited some code which has some very complicated initialization in the constructor that depends on a very complex environment.

I only want to be able to test some functionality therefore just need an empty object, for example one which would have been generated by the default constructor, however the default constructor has been overwritten by some very complex stuff.

I do not have the ability to touch the source code therefore I just need the empty object to be able to call it's functions and test with.

How would I do this? I've looked at mocking but I can't seem to get the actual functionality of the class into the mock object.

UPDATE #1: Example to try to clarify what I'm asking

class Foo(object):
  def __init__(self, lots_of_stuff):
    lotsofthingsbeingdone()

class Bar(Foo):
  def core_functionality(self, something):
    val =  does_something_important(something)
    return val

I want to test core_functionality(). I want to feed it "something" and ensure that the val meets my expectation of what it should be.

Upvotes: 0

Views: 487

Answers (1)

user2357112
user2357112

Reputation: 281252

Use this wisely. Don't make the legacy mess bigger:

# No constructors executed.
empty_object = object.__new__(YourClass)

Upvotes: 1

Related Questions