问题
I have a current codebase that has all of its functions in its view controllers. Meaning in the SignUpViewController, we have a signup function that does the signing up. Moreover, we also have a User model, but it does not have any member signup function.
In order to use mocking and dependency injection during testing, should I refactor the code so that the User model has a signup member function, and the view controller's sign up function just receives a user object and calls the object's member sign up function? Or is there a better alternative?
回答1:
General rule: It it's hard to write the tests, your architecture is most propably not good.
In your case, doing all the work in the view controller is really really bad. Code should be easy to read and easy to change because this is what we developers do all the time.
So you should improve your achitecture. But I would add tests before the refactoring to make sure you don't add bugs. After the refactoring you'll need to change the tests to fit the changed architecture.
来源:https://stackoverflow.com/questions/53662302/should-i-refactor-to-be-able-to-use-mocking-with-xctests