我应该重构以便能够与XCTests一起使用模拟吗? [英] Should I refactor to be able to use mocking with XCTests?

查看:61
本文介绍了我应该重构以便能够与XCTests一起使用模拟吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个当前的代码库,该代码库在其视图控制器中具有所有功能.在SignUpViewController中,这意味着我们有一个注册函数来进行注册.而且,我们也有一个User模型,但是它没有任何成员注册功能.

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.

为了在测试过程中使用模拟和依赖注入,我应该重构代码,以便User模型具有注册成员函数,而视图控制器的注册函数仅接收用户对象并调用该对象的成员注册功能?还是有更好的选择?

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?

推荐答案

一般规则:编写测试很困难,您的体系结构最可能不好.

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.

这篇关于我应该重构以便能够与XCTests一起使用模拟吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆