mvc中viewmodel_在Swift中模拟可测试的ViewModel

mvc中viewmodel

If you’re using MVVM architecture in your iOS project you probably have a ViewModel that gets data from the network. So how can we write tests for that kind of ViewModel?

如果您在iOS项目中使用MVVM体系结构,则可能有一个ViewModel从网络获取数据。 那么我们如何为这种ViewModel编写测试呢?

You should be able to test your App functionality without depending on data from dependencies, Right?

您应该能够测试App功能,而不必依赖于依赖项中的数据,对吗?

In this article, I will show you how you can achieve this.

在本文中,我将向您展示如何实现这一目标。

Before we can write tests for the ViewModel we must make sure that the ViewModel is testable.

在为ViewModel编写测试之前,必须确保ViewModel是可测试的。

To make our code testable we should consider two concepts:

为了使我们的代码可测试,我们应该考虑两个概念:

1。 依赖倒置 (1 . Dependency Inversion)

Dependency Inversion is one the principle of SOLID Design Principles that says :

依赖倒置是SOLID设计原则的原则之一,它说:

High-level modules should not depend on low-level modules; both should depend on abstractions.

高级模块不应依赖于低级模块。 两者都应依赖抽象。

Abstractions should not depend on details. Details should depend upon abstractions.

抽象不应依赖细节。 细节应取决于抽象。

2。 依赖注入 (2 . Dependency Injection)

In software engineering, dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object.

在软件工程中, 依赖项注入是一种技术,通过该技术,一个对象(或静态方法)可以提供另一个对象的依赖项。

Enough with the theory, let’s get our hands dirty by Coding.

有了足够的理论,我们就可以通过编码使我们的手变得肮脏。

Below image demonstrates what we are going to create.

下图演示了我们将要创建的内容。

Abstract Network Layer

抽象网络层

This piece of code is an Abstract layer that contains every method we need to implement in the target classes.

这段代码是一个Abstract层,其中包含我们需要在目标类中实现的所有方法。

We need two methods, one for getting home data and one for comments.

我们需要两种方法,一种用于获取家庭数据,另一种用于评论。

Protocols can be handy for creating an Abstract class.

协议可以很方便地创建Abstract类。

URLSession

URLSession

URLSessionNetwork class is responsible for fetching data from the network using URLSession by implementing methods of Abstract Network Layer.

URLSessionNetwork类负责通过实现抽象网络层的方法使用URLSession从网络中获取数据。

Mock

嘲笑

This class generates fake data by implementing methods of Abstract Network Layer.

此类通过实现抽象网络层的方法来生成伪造数据。

ViewModel

视图模型

Now we can create our ViewModel

现在我们可以创建我们的ViewModel

As you may have seen, the ViewModel constructed by an Abstract Network Layer. But Why? Can we just create it by an empty initializer and then use the URLSession class in methods?

如您所见,ViewModel由抽象网络层构造。 但为什么? 我们可以只通过一个空的初始化程序创建它,然后在方法中使用URLSession类吗?

The answer to the question is something that I just mentioned above that is Dependency Injection.

这个问题的答案就是我上面刚刚提到的依赖注入。

We want to inject our ViewModel to be either a URLSession class or a Mock class. The ViewModel doesn’t care which one of them will be injected as long as they are a subclass of Abstract Network Layer.

我们想将ViewModel注入为URLSession类或Mock类。 只要ViewModel是Abstract Network Layer的子类,ViewModel并不关心将注入其中的哪一个。

注意 (Note)

Notice that all of those classes marked with “final” keyword well it has two reasons:

请注意,所有这些标记有“ final”关键字的类都具有两个原因:

1 . There is no need for overriding those classes.

1。 无需覆盖这些类。

2 . Classes will use Static Dispatch so the performance will improve.

2。 类将使用静态调度,因此性能将得到提高。

If you are curious about Method Dispatch in Swift Programming Language this article will help you through that:

如果您对Swift编程语言中的方法调度感到好奇,那么本文将为您提供帮助:

Alright now our ViewModel is testable

好了,现在我们的ViewModel是可测试的

“Mission Passed “

“任务通过了”

Let’s dive into the writing Unit tests.

让我们深入研究写作单元测试。

Our ViewModel has two functions so we need to write two tests.

我们的ViewModel具有两个功能,因此我们需要编写两个测试。

If you need the complete source code you can find it on the below link.

如果您需要完整的源代码,可以在下面的链接中找到它。

咨询服务 (Consultation)

Before you’re going to write unit tests for your code you must make sure that your code is testable. Dependency Inversion and Dependency Injection will help you through that.If your code has some part that gets data from dependencies then you need to Mock that dependency so you can write unit tests for that.

在为代码编写单元测试之前,必须确保代码是可测试的。 依赖倒置和依赖注入将帮助您解决这个问题。如果您的代码中有一部分可以从依赖中获取数据,则需要模拟该依赖,以便为此编写单元测试。

翻译自: https://medium.com/@mehdiimrz/testable-viewmodel-using-mock-75a25a6b9fb2

mvc中viewmodel

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值