博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Kakapo.js进行动态模拟
阅读量:2521 次
发布时间:2019-05-11

本文共 10125 字,大约阅读时间需要 33 分钟。

by zzarcon

由zzarcon

使用Kakapo.js进行动态模拟 (Dynamic mocking with Kakapo.js)

3 months after the first commit, reaches the first release and we are proud to announce that now it is ready to use. Let us introduce you Kakapo.

首次提交3个月后, 到达了第一个发行版,我们很自豪地宣布现在可以使用了。 让我们向您介绍Kakapo。

Kakapo - The next generation mocking framework in Javascript
Kakapo-Java的下一代模拟框架

Kakapo is just a set of tools that tries to make your life easier when building web apps, specially when creating client side mocks. It provides components and APIs which let you easily replicate your backend logic and responses in the client-side.

Kakapo只是一组工具,可在构建Web应用程序时使您的生活更加轻松,特别是在创建客户端模拟时 。 它提供了组件和API,使您可以轻松地在客户端复制后端逻辑和响应。

This is nothing new and I’m quite sure you are familiar with tools like , or , those tools are great and have been there for quite a loot of time but in my opinion they are just one part of the solution of a big problem.

这并不是什么新鲜事,我敢肯定,您熟悉诸如 , 或的工具,这些工具很棒,并且已经存在了很长一段时间,但在我看来,它们只是其中的一部分解决一个大问题。

Why should you care about client side mocking? To solve the Backend-Bottleneck.

您为什么要关心客户端嘲笑? 解决Backend-Bottleneck问题

后端瓶颈 (Backend Bottleneck)

Last sprint retrospective, after the third sprint in a row missing more than 50% of the planed points, we were starting to ask ourselves what was going wrong. Some back end devs were saying:

上次冲刺回顾,在连续第三次冲刺错过了超过50%的计划点之后,我们开始问自己出了什么问题。 一些后端开发人员在说:

  • Yeah, we thought it was an easy task but we had to spend 1 week refactoring the current functionality to make it working as expected…

    是的,我们认为这是一项轻松的任务,但是我们不得不花1周的时间来重构当前功能,以使其按预期工作……

  • Too much not planned stuff came out and we had to take care of those issues happening in production…

    太多计划外的东西出来了,我们不得不照顾生产中发生的那些问题……

  • Staging servers were not working at all and we had to re-deploy the service more than 5 times…

    登台服务器根本无法正常工作,我们不得不重新部署该服务超过5次…

On the other side, front end devs:

另一方面,前端开发人员:

  • I spent the whole Monday trying to figure out why the endpoint was returning 500 status code instead of getting the expected response…

    我花了整个星期一试图弄清楚为什么端点返回500状态代码而不是得到预期的响应…

  • We were building the user profile but the create endpoint was not documented, so we couldn’t make it for the release…

    我们正在构建用户个人资料,但未记录创建端点,因此我们无法在发布中使用它…

  • Yesterday I had to switch too many times within different staging environments that I didn’t had time to work on the feature…

    昨天,我不得不在不同的登台环境中切换太多次,以至于我没有时间来使用该功能……

I was very frustrated about the current situation and, specially, not being able to ship a small feature in the estimated time. It took me quite some time to realize that it was not a backend or client problem: the issue was something deeper and would require more time and effort to be fixed.

对于当前的情况,我感到非常沮丧,特别是无法在预计的时间内发布小功能。 我花了相当多的时间才意识到这不是后端或客户问题:这个问题更深层次,需要花费更多时间和精力来解决。

What about not dealing with backend issues and staging environments but instead building the feature based on a JSON response agreed with the backend team beforehand?
不处理后端问题和暂存环境,而是根据事先与后端团队同意的JSON响应来构建功能,该怎么办?

Let’s see a basic example to get an idea of how it works:

让我们看一个基本的例子,以了解其工作原理:

In the example above we are just defining a couple of endpoints and one factory, then we define some business logic inside the request handlers in order to return the fake responses. To do this we use three key elements of Kakapo:

在上面的示例中,我们仅定义了两个端点和一个工厂,然后在请求处理程序中定义了一些业务逻辑,以便返回假响应。 为此,我们使用了Kakapo的三个关键要素:

  • : Kakapo’s router recognizes URLs (routes) and dispatches them to the associated handlers. It also provides a request object as argument that gives you useful information about the incoming request.

    :Kakapo的路由器识别URL(路由),并将其分派到关联的处理程序。 它还提供了一个请求对象作为参数,为您提供有关传入请求的有用信息。

  • : This class along with factories and relationships allows you to define how your entities should be represented and their behaviors.

    :该类与工厂关系一起使您可以定义应如何表示实体及其行为。

  • : It connects all other components and lets you activate or deactivate them; this feature gives you the ability to switch between multiple databases and routers, we call this .

    :它连接所有其他组件,并允许您激活或停用它们; 此功能使您能够在多个数据库和路由器之间切换,我们将其称为“ 。

现实生活中的客户端嘲笑 (Client side mocking in real life)

Usually mocking APIs is done by creating a static JSON for every single request and testing against it. Creating and maintaining the JSON is a repetitive task and error-prone.

通常,通过为每个请求创建静态JSON并对其进行测试来完成模拟API。 创建和维护JSON是一项重复性任务,容易出错。

Kakapo, instead, lets you dynamically mock your responses by defining how they should look like and automatically serialize them into JSON.

相反,Kakapo允许您通过定义响应的外观来动态模拟响应,并自动将其序列化为JSON。

As an example, let’s try to make a CRUD

例如,让我们尝试制作CRUD

That’s how easy is to replicate a CRUD with Kakapo, you might also take a look at and to see some goodies of Kakapo.

用Kakapo复制CRUD很容易,您还可以查看和以了解Kakapo的一些优点。

技术挑战 (Technical challenges)

Besides all the stuff we have learned in the process of building the library, I would like to point out some of the most challenging things we had to face:

除了我们在构建库的过程中学到的所有东西之外,我还要指出一些我们必须面对的最具挑战性的事情:

拦截器 (Interceptors)

The s components are the ones in charge of intercepting the user request, check if matches any of the routes and apply the mock, they are designed in such a pluggable way that the user can define his owns. Currently we are supporting the browser networking APIs, and but soon we will support Node.js ?.

的组件是负责拦截用户请求,检查是否匹配任何路由并应用模拟的组件,它们以可插入的方式设计,用户可以定义自己的。 当前,我们支持浏览器网络API, 和但不久之后我们将支持Node.js?。

Reinventing the wheel is not implicitly bad. You can learn a lot doing it.
重新发明轮子并不是很坏。 您可以学到很多东西。

I found this component complicated because you have to replicate the same functionality that the browser APIs provides, as soon as you behave a bit differently, the application might break since it depends on the native behavior. You can learn a lot while building this stuff directly with native APIs instead of using wrappers like jQuery, because you will really understand how it internally works.

我发现此组件很复杂,因为您必须复制浏览器API提供的相同功能,一旦行为有所不同,应用程序可能会中断,因为它取决于本机行为。 在直接使用本机API而不是像jQuery这样的包装器直接构建这些东西时,您可以学到很多东西,因为您将真正了解它的内部工作原理。

While implementing the interceptors we had to make sure to not break popular networking libraries like and ; we created to ensure that Kakapo will continue to work as expected after introducing new changes.

在实现拦截器时,我们必须确保不要破坏流行的联网库,例如和 ; 我们创建了 以确保Kakapo在进行新更改后将继续按预期工作。

测试中 (Testing)

Testing is a must when developing software, but is even more critical in open source projects that other developers will potentially use. We always had this in mind while creating Kakapo and this was the first project I ever did in a strict TDD way. I have to admit that the feeling I had at the beginning was way different than the one I have now. Sometimes I felt that writing so many tests was slowing us down, but now with , I feel really confident when I have to refactor a critical component or add a new feature to the library.

在开发软件时,测试是必须的,但在其他开发人员可能使用的开源项目中,测试则更为关键。 在创建Kakapo时,我们始终牢记这一点,这是我有史以来以严格的TDD方式进行的第一个项目。 我必须承认,我一开始的感觉与我现在的感觉有很大不同。 有时,我觉得编写这么多的测试会拖慢我们的脚步,但是现在有了 ,当我必须重构关键组件或向库中添加新功能时,我感到非常有信心。

This is something you have to introduce in your workflow incrementally and define with the team. Since this was the biggest open source project I ever worked on, I learned how to coordinate and work with a team. Sometimes things need to be discussed just to make sure that all the members are on the same page, but at the end is going to work out.

您必须在工作流程中逐步引入并与团队一起定义。 由于这是我从事过的最大的开源项目,因此我学会了如何与团队协调和合作。 有时需要讨论 ,以确保所有成员都在同一页面上,但最后要进行工作。

文件的重要性 (Importance of documentation)

Developers hate writing documentation. Unfortunately, it is as important as having a good library and will be the first thing your users and contributors will see.

开发人员讨厌编写文档。 不幸的是,它与拥有一个好的库同样重要,并且将是您的用户和贡献者首先看到的。

Think about it this way: you have been building your library for some months and now is finally ready, don’t you think it is worth the effort to spend some days and writing some good examples?

这样考虑:您已经建立了几个月的图书馆,现在终于可以使用了,您认为花几天时间并编写一些好的示例值得您付出吗?

This is a talk from React Europe 2016 in which explains how Facebook deal with spreading open source libraries.

这是来自React Europe 2016的一个演讲, 多( 在解释了Facebook如何处理传播的开源库。

杰基尔 (Jekyll)

literally saved us, it improved the way we write documentation and the velocity. Before choosing Jekyll, I used to build static websites with some css and html to then place there the docs. However, some developers might not be fluent and miss the simplicity of . That’s why we decided to go for Jekyll which lets you write your pages in (markdown with steroids) and is integrated with Github Pages.

从字面上拯救了我们,它改善了我们编写文档的方式和速度。 在选择Jekyll之前,我曾经用一些CSS和html构建静态网站,然后将文档放置在那里。 但是,有些开发人员可能不会流利,会错过的简单性。 这就是为什么我们决定选择Jekyll的原因,它可以让您用编写页面(类固醇的降价),并与Github Pages集成。

Once we felt comfortable with the status of the docs and the examples, we also wanted to give a good first impression to newcomers. We created a which fetches the md file from the , adds some content and outputs the ?

一旦我们对文档和示例的状态感到满意,我们还希望给新手一个良好的第一印象。 我们创建了一个 ,该从获取md文件 ,添加了一些内容并输出了 ?

演示应用 (Demo Apps)

Everyone likes demos, they show what your library does and how it does it. It might sound weird, but it will also help you to learn how to use your own library, as well as finding bugs or missing features.

每个人都喜欢演示,他们演示了您的库做什么以及如何做。 听起来可能很奇怪,但是它也将帮助您学习如何使用自己的库,以及发现错误或缺少功能。

Until we built our first using Kakapo we didn’t realize about the major pain points and how to solve them, that’s why we later built our second demo app, a .

直到我们使用Kakapo构建了第一个我们才意识到主要的难点以及如何解决它们,这就是为什么我们后来构建了第二个演示应用程序(一个 。

Having a good library without documentation is like having rocket that nobody knows how to use it.
拥有一个没有文档的好的图书馆就像拥有一个没人知道如何使用它的火箭一样。

路线图 (ROADMAP)

The project just started but we have ambitious plans for it; feel free to check the or open new ones, we will really appreciate it! Here are some of the most important:

这个项目刚刚开始,但是我们有雄心勃勃的计划。 随时检查或发现新的问题,我们将非常感谢! 以下是一些最重要的信息:

  • Full serializer support

    全面的序列化程序支持

  • Node.js interceptors support

    Node.js拦截器支持

  • Async handlers support

    异步处理程序支持

We are also working hard to finish which is almost ready for the beta phase and we think is going to be a game changer to build iOS applications: stay tuned! ?

我们也正在努力完成该几乎已经可以进入Beta测试阶段,我们认为它将成为构建iOS应用程序的颠覆者:敬请期待! ?

翻译自:

转载地址:http://wxgwd.baihongyu.com/

你可能感兴趣的文章
linux实现多台服务器时时同步文件,推荐lsyncd
查看>>
覆盖墙壁
查看>>
入门级(python)
查看>>
Delphi 解析HTML
查看>>
讲讲最近自己的学习,谈谈未来的想法
查看>>
MVC框架2 struts2+ibatis+spring
查看>>
原生安卓去除网络叉号
查看>>
命令行下设置串口并发送
查看>>
SEO前端搜索引擎优化
查看>>
Entity Framework4.0 (四) EF4的内部结构和几个基本概念(转)
查看>>
WCF大数据量传输的详细步骤
查看>>
Objective-C之词典对象
查看>>
数据结构学习笔记3.2—快速排序
查看>>
ShellApi 列举正在运行的程序
查看>>
转:关于JAVA多线程同步
查看>>
Javascript之UI线程与性能优化
查看>>
实现toggleClass功能
查看>>
设计Web2.0图--Aeromatex
查看>>
nginx动静分离配置
查看>>
jQuery 两种方法实现IE10以下浏览器的placeholder效果
查看>>