小程序 ios模拟测试
For most developers, writing tests is the bane of their existence. I’ll even admit it: I really don’t like writing tests…at all. But when I can go to my customer with confidence knowing the application is going to function the way I intended it to because I unit-tested every piece of functionality, well, that feeling is priceless.
对于大多数开发人员而言,编写测试是其存在的祸根。 我什至承认:我真的不喜欢编写测试……。 但是当我可以放心地去拜访客户时,知道应用程序将按照我的预期方式运行,因为我对每个功能进行了单元测试,那么,这种感觉是无价的。
总览 (Overview)
Although generic unit testing is relatively simple, when you start bringing in frameworks and other services into your application (which you will if you’re creating a reasonably sized app), you will need to start writing your code in a way that allows for it to be easily tested through mocking. Once we do that, we can write tests in a way that mocks how the services act without actually using them. As a result, we can test the functionality of our application at a smaller unit scale without having to rely on the service that we’re utilizing in production while we run our tests.
尽管通用单元测试相对简单,但是当您开始在应用程序中引入框架和其他服务时(如果要创建大小合适的应用程序,则需要这样做),您将需要以一种允许的方式开始编写代码。可以通过模拟轻松进行测试。 一旦做到这一点,我们就可以编写模拟测试的方式来测试服务,而无需实际使用它们。 结果,我们可以在较小的单元规模上测试应用程序的功能,而不必在运行测试时依赖生产中使用的服务。
Throughout this article, I’m going to demonstrate how to write code that mocks a trivial cloud function messaging service. In production, this would update a database upon successful completion of the message being sent. You will see that you do not need to know how this cloud function messaging service is implemented and can still test the view model at a unit scale by mocking the output of the modules we depend on.
在本文中,我将演示如何编写模拟琐碎的云函数消息传递服务的代码。 在生产中,这将在成功完成发送消息后更新数据库。 您将看到您不需要了解此云功能消息传递服务的实现方式,并且仍然可以通过模拟我们依赖的模块的输出来以单位规模测试视图模型。
什么是模拟? (What Is Mocking?)
Mocking in software is no different than mocking in the real world. When someone/something is mocked in the real world, the way that person/thing acts or performs is being mimicked.
在软件中进行模拟与在现实世界中进行模拟没有什么不同。 当某人/某物在现实世界中被嘲笑时,该人/某物的行为或行为方式就会被模仿。
In software testing, when we mock a service or a module, we’re replicating the way a module/service acts, which allows us to simulate the behavior of a module during testing without having to use the module itself. Consequently, we can make assertions on the module that we’re testing by mocking all dependencies of the module being tested. By doing so, we will be able to pinpoint errors within the module we’re testing and not worry about potential issues stemming from other modules. If this weren’t the case, it could be very hard to tell if the test was failing due to the current module’s code or code from another module!
在软件测试中,当我们模拟服务或模块时,我们正在复制模块/服务的