You are currently viewing Creating fakes using reflection

Creating fakes using reflection

We use a third party dependency in our project for analytics. Previously, we were mocking the object for tests, but want to move to fakes now. For most dependencies, we’ve been able to achieve this by extending the class and mocking it’s constructor parameters when required.

The problem with this particular object is that it takes a thousand constructor parameters and does a fair amount of work during construction so even simple mocking of all the parameters doesn’t work.

I tried to look for way to do this using reflection but haven’t been successful. Does anyone know of a way to set the type of a class using reflection? Or indeed another way to achieve the goal of creating a fake object of a particular type without invoking it’s constructor.

submitted by /u/unimpeccable
[link] [comments]