From ccafae69836ca05885747482d3e13340f42e0b2c Mon Sep 17 00:00:00 2001 From: Pingren Nie Date: Mon, 16 Sep 2019 14:36:58 +0800 Subject: [PATCH 01/10] =?UTF-8?q?=E5=A6=82=E4=BD=95=E4=BD=BF=E7=94=A8=20Mo?= =?UTF-8?q?untebank=20=E5=92=8C=20Node.js=20=E6=9D=A5=20Mock=20=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 如何使用 Mountebank 和 Node.js 来 Mock 服务 --- ...k-services-using-mountebank-and-node-js.md | 222 +++++++++--------- 1 file changed, 111 insertions(+), 111 deletions(-) diff --git a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md index 390e4851575..48ee7cfcc57 100644 --- a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md +++ b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md @@ -2,77 +2,77 @@ > * 原文作者:[Dustin Ewers](https://www.digitalocean.com/community/users/dustinjewers) > * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) > * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO1/how-to-mock-services-using-mountebank-and-node-js.md](https://github.com/xitu/gold-miner/blob/master/TODO1/how-to-mock-services-using-mountebank-and-node-js.md) -> * 译者: +> * 译者:[Pingren](https://github.com/Pingren) > * 校对者 -# How To Mock Services Using Mountebank and Node.js +# 如何使用 Mountebank 和 Node.js 来 Mock 服务 -## Introduction +## 前言 -In complex [service-oriented architectures (SOA)](https://en.wikipedia.org/wiki/Service-oriented_architecture), programs often need to call multiple services to run through a given workflow. This is fine once everything is in place, but if the code you are working on requires a service that is still in development, you can be stuck waiting for other teams to finish their work before beginning yours. Additionally, for testing purposes you may need to interact with external vendor services, like a weather API or a record-keeping system. Vendors usually don't give you as many environments as you need, and often don't make it easy to control test data on their systems. In these situations, unfinished services and services outside of your control can make code testing frustrating. +复杂的[面向服务架构(SOA)](https://en.wikipedia.org/wiki/Service-oriented_architecture)程序中,通常需要调用多个服务来运行一个完整的工作流。尽管一切服务就绪时没有问题,但如果你的代码依赖一个正在开发的服务,你不得不等待其它团队完成任务之后才能开始工作。此外,你可能需要使用外部供应商的服务,比如天气 API 或者记录系统。供应商通常不会提供足够的环境供你使用,控制他们系统的测试数据也不容易。面对这些未完成的和没有控制权的服务,代码测试让人感到沮丧。 -The solution to all of these problems is to create a *service mock*. A service mock is code that simulates the service that you would use in the final product, but is lighter weight, less complex, and easier to control than the actual service you would use in production. You can set a mock service to return a default response or specific test data, then run the software you're interested in testing as if the dependent service were really there. Because of this, having a flexible way to mock services can make your workflow faster and more efficient. +解决这些问题的办法是创建一个 **服务 mock**。服务 mock 用于模拟最终产品中提供的服务,但相对真正的服务而言更加轻量、简单且易于控制。你可以设置 mock 服务的响应的默认值,或者设置返回特定的测试数据,然后就可以运行你想要测试的程序,因为其依赖的服务已经就绪。如此一来,灵活的 mock 服务使你的工作流更加迅速高效。 -In an enterprise setting, making mock services is sometimes called *service virtualization*. Service virtualization is often associated with expensive enterprise tools, but you don't need an expensive tool to mock a service. [Mountebank](http://www.mbtest.org/) is a free and open source service-mocking tool that you can use to mock HTTP services, including [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) and [SOAP](https://en.wikipedia.org/wiki/SOAP) services. You can also use it to mock [SMTP](https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol) or [TCP](https://en.wikipedia.org/wiki/Transmission_Control_Protocol) requests. +在企业环境中,创建 mock 服务有时候也叫服务虚拟化。服务虚拟化通常与昂贵的企业级工具有关,但你并不需要昂贵的工具来 mock 服务。[Mountebank](http://www.mbtest.org/)是一个免费并开源的服务 mock 工具。你可以用它 mock HTTP 服务,包括 [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) 和 [SOAP](https://en.wikipedia.org/wiki/SOAP) 服务。你还可以用它 mock [SMTP](https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol) 或 [TCP](https://en.wikipedia.org/wiki/Transmission_Control_Protocol) 请求。 -In this guide, you will build two flexible service-mocking applications using [Node.js](https://nodejs.org/en/about/) and Mountebank. Both of the mock services will listen to a specific port for REST requests in HTTP. In addition to this simple mocking behavior, the service will also retrieve mock data from a [*comma-separated values* (CSV) file](https://en.wikipedia.org/wiki/Comma-separated_values). After this tutorial, you'll be able to mock all kinds of service behavior so you can more easily develop and test your applications. +在本教程中,你将使用 [Node.js](https://nodejs.org/en/about/) 和 Mountebank 搭建两个灵活的服务 mock 程序。它们都将监听一个特定的端口的 HTTP REST 请求。除了这个简单的 mock 行为之外,这个服务还将从 [*逗号分隔值* (CSV) 文件](https://en.wikipedia.org/wiki/Comma-separated_values)中获取 mock 数据。完成教程后,你将能 mock 各种各样的服务行为,更轻松地开发和测试程序。 -### Prerequisites +### 前提 -To follow this tutorial, you will need the following: +为了学习本教程,你需要: -- Version 8.10.0 or higher of Node.js installed on your machine. This tutorial will use version 8.10.0. To install Node.js, check out [How To Install Node.js on Ubuntu 18.04](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-18-04) or [How to Install Node.js and Create a Local Development Environment on macOS](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-and-create-a-local-development-environment-on-macos). -- A tool to make HTTP requests, like [cURL](https://curl.haxx.se/) or [Postman](https://www.getpostman.com/). This tutorial will use cURL, since it's installed by default on most machines; if your machine does not have cURL, please see the[install documentation](https://curl.haxx.se/docs/install.html). +- 在你的机器上安装 8.10.0 及以上版本的 Node.js。本教程将使用 8.10.0 版。要安装 Node.js,请查看[如何在 Ubuntu 18.04 上安装 Node.js](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-18-04) 或 [如何在 macOS 上安装 Node.js 和创建本地开发环境](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-and-create-a-local-development-environment-on-macos)。 +- 发送 HTTP 请求的工具,比如 [cURL](https://curl.haxx.se/) 或 [Postman](https://www.getpostman.com/)。本教程将使用 cURL,因为它默认安装在大多数机器上;如果你的机器上没有 cURL, 请看看这个[安装文档](https://curl.haxx.se/docs/install.html)。 -### Step 1 --- Starting a Node.js Application +### 第 1 步 —— 启动 Node.js 程序 -In this step, you are going to create a basic Node.js application that will serve as the base of your Mountebank instance and the mock services you will create in later steps. +此步骤中,你将创建一个基本的 Node.js 程序,作为你在后续步骤中创建的 Mountebank 实例以及 mock 服务的基础。 -Note: Mountebank can be used as a standalone application by installing it globally using the command `npm install -g mountebank`. You can then run it with the `mb` command and add mocks using REST requests. +请注意:你可以使用命令 `npm install -g mountebank` 全局安装 Mountebank ,将其作为独立的应用程序。然后你可以使用 `mb` 命令运行它,并用 REST 请求添加 mock。 -While this is the fastest way to get Mountebank up and running, building the Mountebank application yourself allows you to run a set of predefined mocks when the app starts up, which you can then store in source control and share with your team. This tutorial will build the Mountebank application manually to take advantage of this. +虽然这是运行 Mountebank 的最快方法,自行构建的 Mountebank 程序可以在启动时运行一系列的预定义 mock,而这些又可以被保存在源码管理中并与团队共享。本教程将会手动构建有这种优点的 Mountebank 程序。 -First, create a new directory to put your application in. You can name it whatever you want, but in this tutorial we'll name it `app`: +首先,创建一个新目录保存你的程序。你可以根据需要命名,但在本教程中我们将它命名为 `app`: ```bash mkdir app ``` -Move into your newly created directory with the following command: +使用以下命令进入新创建的目录: ```bash cd app ``` -To start a new Node.js application, run `npm init` and fill out the prompts: +为了启动新的 Node.js 应用程序,请运行 `npm init` 并根据提示继续: ```bash npm init ``` -The data from these prompts will be used to fill out your `package.json` file, which describes what your application is, what packages it relies on, and what different scripts it uses. In Node.js applications, scripts define commands that build, run, and test your application. You can go with the defaults for the prompts or fill in your package name, version number, etc. +根据提示输入的数据将用于填写你的 `package.json` 文件,这个文件描述了你的程序,它的依赖,以及它使用的不同脚本。在一个 Node.js 程序中,脚本定义了构建,运行,测试程序的命令。你可以使用提示中的默认值,或者手动填写你的包名,版本号等信息。 -After you finish this command, you'll have a basic Node.js application, including the `package.json` file. +在你完成这条命令后,你将得到一个基本的 Node.js 程序,包括这个 `package.json` 文件。 -Now install the Mountebank npm package using the following: +现在使用以下命令安装 Mountebank npm 软件包: ```bash npm install -save mountebank ``` -This command grabs the Mountebank package and installs it to your application. Make sure to use the `-save` flag in order to update your `package.json` file with Mountebank as a dependency. +这条命令将获取 Mountebank 软件包并将它安装到你的程序中。请确保使用 `-save` 标记更新你的 `package.json` 文件,使它将 Mountebank 作为依赖。 -Next, add a start script to your `package.json` that runs the command `node src/index.js`. This script defines the entry point of your app as `index.js`, which you'll create in a later step. +接下来,向你的 `package.json` 添加一个执行 `node src/index.js` 的启动脚本。此脚本将 `index.js` 定义为程序入口,你将在之后的步骤中创建它。 -Open up `package.json` in a text editor. You can use whatever text editor you want, but this tutorial will use nano. +用文本编辑器打开 `package.json`。你可以使用你喜欢的文本编辑器,但是本教程中将使用 nano。 ```bash nano package.json ``` -Navigate to the `"scripts"` section and add the line `"start": "node src/index.js"`. This will add a `start` command to run your application. +定位到 `"scripts"` 部分并添加 `"start": "node src/index.js"`。 -Your `package.json` file should look similar to this, depending on how you filled in the initial prompts: +你的 `package.json` 文件应该和它类似,这取决于你如何填写初始的命令提示: ```json { @@ -91,33 +91,33 @@ Your `package.json` file should look similar to this, depending on how you fil } ``` -You now have the base for your Mountebank application, which you built by creating your app, installing Mountebank, and adding a start script. Next, you'll add a settings file to store application-specific settings. +通过创建你的程序,安装 Mountebank 并添加一个启动脚本,你现在拥有了 Mountebank 程序的基础。接下来,你将添加一个配置文件,来保存程序特定的设置。 -### Step 2 --- Creating a Settings File +### 第 2 步 —— 创建配置文件 -In this step, you will create a settings file that determines which ports the Mountebank instance and the two mock services will listen to. +此步骤中,你将创建一个配置文件,它决定了 Mountebank 实例和两个 mock 服务将会监听的端口。 -Each time you run an instance of Mountebank or a mock service, you will need to specify what network port that service will run on (e.g., `http://localhost:5000/`). By putting these in a settings file, the other parts of your application will be able to import these settings whenever they need to know the port number for the services and the Mountebank instance. While you could directly code these into your application as constants, changing the settings later will be easier if you store them in a file. This way, you will only have to change the values in one place. +每次运行 Mountebank 实例或 mock 服务时,你将需要指定服务所使用的网络端口(例如,`http://localhost:5000/`)。将这些放在配置文件中,程序的其他部分将能够在它们需要时导入服务和 Mountebank 实例的端口号。你可以直接将它作为常量编写到你的程序中,但储存在文件中使得未来修改配置更简单。这样一来,你只需要更改一处的值。 -Begin by making a directory called `src` from your `app` directory: +首先,从 `app` 目录中创建一个 `src` 目录: ```bash mkdir src ``` -Navigate to the folder you just created: +导航到刚创建的文件夹: ```bash cd src ``` -Create a file called `settings.js` and open it in your text editor: +创建名为 `settings.js` 的文件并在你的文本编辑器中打开它: ```bash nano settings.js ``` -Next, add settings for the ports for the main Mountebank instance and the two mock services you'll create later: +接下来,为你将要创建的主 Mountebank 实例和两个 mock 服务添加端口配置: ```js module.exports = { @@ -127,21 +127,21 @@ module.exports = { } ``` -This settings file has three entries: `port: 5000` assigns port `5000` to the main Mountebank instance, `hello_service_port: 5001` assigns port `5001` to the Hello World test service that you will create in a later step, and `customer_service_port: 5002` assigns port `5002` to the mock service app that will respond with CSV data. If the ports here are occupied, feel free to change them to whatever you want. `module.exports =` makes it possible for your other files to import these settings. +这个配置文件有三个条目:`port: 5000` 将端口 `5000` 分配给主 Mountebank 实例,`hello_service_port: 5001` 将端口 `5001` 分配给你将创建的 Hello World 测试服务,`customer_service_port: 5002` 将端口 `5002` 分配给使用 CSV 响应的 mock 服务。如果这些端口被占用,请根据需要修改它们。`module.exports =` 使你的其它文件可以导入这些配置。 -In this step, you used `settings.js` to define the ports that Mountebank and your mock services will listen to and made these settings available to other parts of your app. In the next step, you will build an initialization script with these settings to start Mountebank. +此步骤中,你用了 `settings.js` 来定义 Mountebank 和两个 mock 服务将会监听的端口,并使其可用于你程序的其它部分。接下来,你将会使用这些配置来构建初始化脚本,启动 Mountebank。 -### Step 3 --- Building the Initialization Script +### 第 3 步 —— 构建初始化脚本 -In this step, you're going to create a file that starts an instance of Mountebank. This file will be the entry point of the application, meaning that, when you run the app, this script will run first. You will add more lines to this file as you build new service mocks. +此步骤中,你将创建一个文件来启动 Mountebank 实例。这个文件将会成为程序的入口,即当你运行程序时,这个脚本将会首先运行。当你构建新的服务 mock时,你将向此文件添加更多内容。 -From the `src` directory, create a file called `index.js` and open it in your text editor: +在 `src` 目录中,创建名为 `index.js` 的文件并在你的文本编辑器中打开它: ```bash nano index.js ``` -To start an instance of Mountebank that will run on the port specified in the `settings.js` file you created in the last step, add the following code to the file: +为了启动一个 Mountebank 实例,并使其运行在上一步创建的 `settings.js` 文件中指定的端口上,请向此文件添加以下的代码: ```js const mb = require('mountebank'); @@ -156,33 +156,33 @@ const mbServerInstance = mb.create({ }); ``` -This code does three things. First, it imports the Mountebank npm package that you installed earlier (`const mb = require('mountebank');`). Then, it imports the settings module you created in the previous step (`const settings = require('./settings');`). Finally, it creates an instance of the Mountebank server with `mb.create()`. +这段代码做了三件事。首先,它导入了你之前安装的 Mountebank npm 软件包(`const mb = require('mountebank');`)。接着,它导入了你在上一步创建的配置模块(`const settings = require('./settings');`)。最后,它使用 `mb.create()` 创建了一个 Moutebank 服务器实例。 -The server will listen at the port specified in the settings file. The `pidfile`, `logfile`, and `protofile` parameters are for files that Mountebank uses internally to record its process ID, specify where it keeps its logs, and set a file to load custom protocol implementations. The `ipWhitelist` setting specifies what IP addresses are allowed to communicate with the Mountebank server. In this case, you're opening it up to any IP address. +这个服务器将会监听配置文件中指定的端口,文件中的 `pidfile`,`logfile` 和 `protofile` 参数用于 Mountebank 内部记录它的进程 ID,指定日志位置,和设置加载自定义协议实现的文件。`ipWhitelist` 设置指定了允许与Moutebank 服务器通信的 IP 地址。在这个例子中,你将允许所有 IP 地址。 -Save and exit from the file. +保存并关闭文件。 -After this file is in place, enter the following command to run your application: +在这个文件就绪后,输入以下命令来运行你的程序: ```bash npm start ``` -The command prompt will disappear, and you will see the following: +命令提示符将会消失,你将看到以下内容: ``` info: [mb:5000] mountebank v2.0.0 now taking orders - point your browser to http://localhost:5000/ for help ``` -This means your application is open and ready to take requests. +这代表着你的程序已经打开并准备好接收请求。 -Next, check your progress. Open up a new terminal window and use `curl` to send the following `GET` request to the Mountebank server: +接下来,检查你的进度。打开一个新的终端窗口并使用 `curl` 向 Moutebank 服务器发送以下 `GET` 请求: ```bash curl http://localhost:5000/ ``` -This will return the following JSON response: +这将返回以下 JSON 响应: ```json Output{ @@ -200,35 +200,35 @@ Output{ } ``` -The JSON that Mountebank returns describes the three different endpoints you can use to add or remove objects in Mountebank. By using `curl` to send reqests to these endpoints, you can interact with your Mountebank instance. +Mountebank 返回的 JSON 描述了可添加或删除对象的三个不同端点。通过使用 `curl` 向这些端点发送请求,你可以与 Mountebank 实例交互。 -When you're done, switch back to your first terminal window and exit the application using `CTRL` + `C`. This exits your Node.js app so you can continue adding to it. +当你完成了,切换回你的你一个终端窗口并使用 `CTRL` + `C` 退出程序。这将退出你的 Node.js 程序以便继续添加和修改功能。 -Now you have an application that successfully runs an instance of Mountebank. In the next step, you will create a Mountebank client that uses REST requests to add mock services to your Mountebank application. +现在你有了一个成功运行 Mountebank 实例的程序。接下来,你将会创建一个使用 REST 请求添加 mock 服务的 Mountebank 客户端。 -### Step 4 --- Building a Mountebank Client +### 第 4 步 —— 构建 Mountebank 客户端 -Mountebank communicates using a REST API. You can manage the resources of your Mountebank instance by sending HTTP requests to the different endpoints mentioned in the last step. To add a mock service, you send a HTTP `POST` request to the imposters endpoint. An [*imposter*](http://www.mbtest.org/docs/mentalModel) is the name for a mock service in Mountebank. Imposters can be simple or complex, depending on the behaviors you want in your mock. +Mountebank 使用 REST API 通信。你可以向上一步中提过的不同端点发送 HTTP 请求来管理 Mountebank 实例的资源。要添加 mock 服务,[*imposter*](http://www.mbtest.org/docs/mentalModel) 代表 Mountebank 中的 mock 服务。取决于你想在 mock 中实现的行为,Imposters 可以是简单或者复杂的。 -In this step, you will build a Mountebank client to automatically send `POST` requests to the Mountebank service. You could send a `POST` request to the imposters endpoint using `curl` or Postman, but you'd have to send that same request every time you restart your test server. If you're running a sample API with several mocks, it will be more efficient to write a client script to do this for you. +此步骤中,你将构建一个向 Mountebank 服务自动发送 `POST` 请求的 Mountebank 客户端。你可以使用 `curl` 或者 Postman 向 imposters 端点发送一个 `POST` 请求,但是你每次重启测试服务器的时候,必须发送相同的请求。如果你正在运行一个具有多个 mock 的 API 示例,编写一个客户端脚本执行这个操作将会更有效率。 -Begin by installing the `node-fetch` library: +首先安装 `node-fetch`: ```bash npm install -save node-fetch ``` -The [`node-fetch` library](https://www.npmjs.com/package/node-fetch) gives you an implementation of the JavaScript Fetch API, which you can use to write shorter HTTP requests. You could use the standard `http` library, but using `node-fetch` is a lighter weight solution. +[`node-fetch` 库](https://www.npmjs.com/package/node-fetch) 提供了一个 JavaScript Fetch API 的实现,你可以使用它来编写更简短的 HTTP 请求。你可以使用标准的 `http` 库,但是使用 `node-fetch` 是一个更加轻量的方案。 -Now, create a client module to send requests to Mountebank. You only need to post imposters, so this module will have one method. +现在,创建一个向 Mountebank 发送请求的客户端模块。你只需要向 imposters 发送 post 请求,因此这个模块将只有一个方法。 -Use `nano` to create a file called `mountebank-helper.js`: +使用 `nano` 创建一个名为 `mountebank-helper.js` 的文件: ```bash nano mountebank-helper.js ``` -To set up the client, put the following code in the file: +为了设置客户端,将以下代码放入这个文件中: ```js const fetch = require('node-fetch'); @@ -247,27 +247,27 @@ function postImposter(body) { module.exports = { postImposter }; ``` -This code starts off by pulling in the `node-fetch` library and your settings file. This module then exposes a function called `postImposter` that posts service mocks to Mountebank. Next, `body:`determines that the function takes `JSON.stringify(body)`, a JavaScript object. This object is what you're going to `POST` to the Mountebank service. Since this method is running locally, you run your request against `127.0.0.1` (`localhost`). The fetch method takes the object sent in the parameters and sends the `POST` request to the `url`. +这段代码首先导入了 `node-fetch` 库和你的配置文件。接着这个模块公开声明了一个函数 `postImposter`向 Mountebank 发送 post 请求。接着,`body:` 决定了这个函数将会使用 `JSON.stringify(body)`,一个 JavaScript 对象。这是你将向 Mountebank 服务 `POST` 的对象。由于这个方法在本地运行,你的请求将会发送至 `127.0.0.1`(`localhost`)。fetch 方法使用了参数中的对象并向 `url` 发送 `POST` 请求。 -In this step, you created a Mountebank client to post new mock services to the Mountebank server. In the next step, you'll use this client to create your first mock service. +此步骤中,你创建了一个 Mountebank 客户端,它向 Mountebank 服务器提交新的 mock 服务。在下一步中,你将使用这个客户端创建你的第一个 mock 服务。 -### Step 5 --- Creating Your First Mock Service +### 第 5 步 —— 创建你的第一个 mock 服务 -In previous steps, you built an application that creates a Mountebank server and code to call that server. Now it's time to use that code to build an imposter, or a mock service. +在之前的步骤中,你构建了一个程序,创建 Mountebank 服务器并编写了请求服务器的代码。现在是时候使用这些代码来构建一个 imposter,即 mock 服务。 -In Mountebank, each imposter contains [*stubs*](http://www.mbtest.org/docs/mentalModel). Stubs are configuration sets that determine the response that an imposter will give. Stubs can be further divided into combinations of [*predicates*and *responses*](http://www.mbtest.org/docs/mentalModel). A predicate is the rule that triggers the imposter's response. Predicates can use lots of different types of information, including URLs, request content (using XML or JSON), and HTTP methods. +在 Mountebank 中,每个 imposter 包含了 [*stubs*](http://www.mbtest.org/docs/mentalModel)。Stubs 是决定 imposter 返回内容的配置集合。Stubs 可以被细分成多个 [*断言*和*响应*](http://www.mbtest.org/docs/mentalModel) 的组合。断言是触发 imposter 响应的规则。断言可以使用许多不同类型的信息,包括了 URL,请求内容(使用 XML 或 JSON),以及 HTTP 方法。 -Looked at from the point of view of a [Model-View-Controller (MVC)](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller) app, an imposter acts like a controller and the stubs like actions within that controller. Predicates are routing rules that point toward a specific controller action. +从[模型 - 视图 - 控制器(MVC)](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller)程序的角度来看,imposter 像控制器而 stubs 像控制器中的行为。断言则是指向特定控制器的行为的路由规则。 -To create your first mock service, create a file called `hello-service.js`. This file will contain the definition of your mock service. +要创建你的第一个 mock 服务,请创建一个名为 `hello-service.js` 的文件。这个文件将包含了 mock 服务的定义。 -Open `hello-service.js` in your text editor: +在文本编辑器中打开 `hello-service.js`: ```bash nano hello-service.js ``` -Then add the following code: +接着添加以下代码: ```js const mbHelper = require('./mountebank-helper'); @@ -310,16 +310,16 @@ function addService() { module.exports = { addService }; ``` -This code defines an imposter with a single stub that contains a predicate and a response. Then it sends that object to the Mountebank server. This code will add a new mock service that listens for `GET` requests to the root `url` and returns `{ message: "hello world" }` when it gets one. +这段代码定义了一个 imposter,它使用了一个含有一个断言和一个响应的 stub。接着它将这个对象发送至 Mountebank 服务器。这段代码将会添加一个在根 `url` 监听 `GET` 请求并在触发时返回 `{ message: "hello world" }`的 mock 服务。 -Let's take a look at the `addService()` function that the preceding code creates. First, it defines a response message `hello world`: +一起来看看以上代码中的 `addService()` 函数。首先,它定义了一条响应消息 `hello world`: ```js const response = { message: "hello world" } ... ``` -Then, it defines a stub: +接着,它定义了一个 stub: ```js ... @@ -347,9 +347,9 @@ Then, it defines a stub: ... ``` -This stub has two parts. The predicate part is looking for a `GET` request to the root (`/`) URL. This means that `stubs` will return the response when someone sends a `GET` request to the root URL of the mock service. The second part of the stub is the `responses` array. In this case, there is one response, which returns a JSON result with an HTTP status code of `200`. +这个 stub 有两个部分。断言部分查找一个根(`/`)URL 的 `GET` 请求。这意味着这个 `stub` 将在有人向 mock 服务的根 URL 发送 `GET` 请求时返回响应。stub 的第二部分是 `responses` 数组。在这个例子中,将会返回一个 HTTP 状态码为`200` 的 JSON 结果。 -The final step defines an imposter that contains that stub: +最后一步定义了含有这个 stub 的 imposter。 ```js ... @@ -361,9 +361,9 @@ The final step defines an imposter that contains that stub: ... ``` -This is the object you're going to send to the `/imposters` endpoint to create an imposter that mocks a service with a single endpoint. The preceding code defines your imposter by setting the `port` to the port you determined in the settings file, setting the `protocol` to HTTP, and assigning `stubs` as the imposter's stubs. +这是你将发往 `/imposters` 端点的对象,用于创建 mock 单个端点服务的 imposter。通过将 `port` 设置为你在配置文件中决定的端口,`protocol` 设置为 HTTP,`stubs`设置为 imposter 的 stubs,以上代码定义了 imposter。 -Now that you have a mock service, the code sends it to the Mountebank server: +现在你有了一个 mock 服务,以下代码将它发往 Mountebank 服务器: ```js ... @@ -371,17 +371,17 @@ Now that you have a mock service, the code sends it to the Mountebank server: ... ``` -As mentioned before, Mountebank uses a REST API to manage its objects. The preceding code uses the `postImposter()` function that you defined earlier to send a `POST` request to the server to activate the service. +如之前所述,Mountebank 使用 REST API 来管理它的对象。这段代码使用你之前定义的 `postImposter()` 函数向服务器发送一个 `POST` 请求来激活这个服务。 -Once you are finished with `hello-service.js`, save and exit from the file. +当你完成了 `hello-service.js`,保存并关闭文件。 -Next, call the newly created `addService()` function in `index.js`. Open the file in your text editor: +接下来,在 `index.js` 中调用刚创建的 `addService()` 函数。在文本编辑器中打开文件: ```bash nano index.js ``` -To make sure that the function is called when the Mountebank instance is created, add the following highlighted lines: +为了确保 Mountebank 实例创建时调用这个函数,添加以下高亮行: ```js const mb = require('mountebank'); @@ -401,55 +401,55 @@ mbServerInstance.then(function() { }); ``` -When a Mountebank instance is created, it returns a *promise*. A promise is an object that does not determine its value until later. This can be used to simplify asynchronous function calls. In the preceding code, the `.then(function(){...})` function executes when the Mountebank server is initialized, which happens when the promise resolves. +当 Mountebank 实例创建时,它返回一个 *promise*。promise 是一个稍后才能确定其值的对象。这可以用于简化异步函数调用。以上代码中,`.then(function(){...})` 函数在 Mountebank 服务器初始化时才执行,而这在 promise resolve 时发生。 -Save and exit `index.js`. +保存并关闭 `index.js`。 -To test that the mock service is created when Mountebank initializes, start the application: +为了测试 Mountebank 初始化时创建了这个 mock 服务,启动程序: ```bash npm start ``` -The Node.js process will occupy the terminal, so open up a new terminal window and send a `GET`request to `http://localhost:5001/`: +Node.js 进程将会占领终端,所以打开一个新的终端窗口并向 `http://localhost:5001/` 发送 `GET` 请求: ```bash curl http://localhost:5001 ``` -You will receive the following response, signifying that the service is working: +你将会接收以下响应,这表明了这个服务正常运行: ``` Output{"message": "hello world"} ``` -Now that you tested your application, switch back to the first terminal window and exit the Node.js application using `CTRL` + `C`. +现在你已经测试了你的程序,切换回第一个终端窗口并使用 `CTRL` + `C` 退出 Node.js 程序。 -In this step, you created your first mock service. This is a test service mock that returns `hello world` in response to a `GET` request. This mock is meant for demonstration purposes; it doesn't really give you anything you couldn't get by building a small Express application. In the next step, you'll create a more complex mock that takes advantage of some of Mountebank's features. +此步骤中,你创建了你的第一个 mock 服务。这是一个对 `GET` 请求响应 `hello world` 测试 mock。这个 mock 主要是为了展示而创建的;实际上,即使不用它,你通过构建一个小型 Express 程序能达到同样的结果。下一步中,你将创建一个更加复杂的 mock,它利用了一些 Mountebank 的功能。 ### Step 6 --- Building a Data-Backed Mock Service -While the type of service you created in the previous step is fine for some scenarios, most tests require a more complex set of responses. In this step, you're going to create a service that takes a parameter from the URL and uses it to look up a record in a CSV file. +尽管你在上一步创建的服务类型足以应付一些场景,绝大多数的测试需要更复杂的响应。此步骤中,你将创建一个服务,它使用 URL 中的参数查询 CSV 文件中的一条记录。 -First, move back to the main `app` directory: +首先,导航至主 `app` 目录: ```bash cd ~/app ``` -Create a folder called `data`: +创建一个名为 `data` 的文件夹: ```bash mkdir data ``` -Open a file for your customer data called `customers.csv`: +打开一个名为 `customers.csv` 的顾客数据文件: ```bash nano data/customers.csv ``` -Add in the following test data so that your mock service has something to retrieve: +添加以下测试数据以便你的 mock 服务检索: ```csv id,first_name,last_name,email,favorite_color @@ -459,17 +459,17 @@ id,first_name,last_name,email,favorite_color 4,Jo,Goulborne,jgoulbornee@example.com,Red ``` -This is fake customer data generated by the API mocking tool [Mockaroo](https://mockaroo.com/), similar to the fake data you'd load into a customers table in the service itself. +这是一个由 API mocking 工具 [Mockaroo](https://mockaroo.com/)生成的假客户数据,类似你将加载到服务本身的顾客表中的假数据。 -Save and exit the file. +保存并关闭文件。 -Then, create a new module called `customer-service.js` in the `src` directory: +接着,在 `src` 目录下创建一个名为 `customer-service.js` 的新模块: ```bash nano src/customer-service.js ``` -To create an imposter that listens for `GET` requests on the `/customers/` endpoint, add the following code: +为了创建一个在 `/customers/` 端点上监听 `GET` 请求的 imposter,添加以下代码: ```js const mbHelper = require('./mountebank-helper'); @@ -528,9 +528,9 @@ function addService() { module.exports = { addService }; ``` -This code defines a service mock that looks for `GET` requests with a URL format of `customers/`. When a request is received, it will query the URL for the `id` of the customer and then return the corresponding record from the CSV file. +这段代码定义了一个服务 mock,它寻找 URL 格式为`customers/`的 `GET` 请求。当收到一个请求时,它将在 URL 上查询顾客的 `id`,并从 CSV 文件里返回相应的记录。 -This code uses a few more Mountebank features than the `hello` service you created in the last step. First, it uses a feature of Mountebank called [*behaviors*](http://www.mbtest.org/docs/mentalModel). Behaviors are a way to add functionality to a stub. In this case, you're using the `lookup` behavior to look up a record in a CSV file: +相比上一步创建的 `hello` 服务,这段代码使用了更多的 Mountebank 功能。首先,它用了 Mountebank的 [*behaviors*](http://www.mbtest.org/docs/mentalModel)。行为是一种为 stub 添加功能的方式。这个例子中,你正在使用 `lookup` 行为来查询 CSV 文件中的一条记录: ```js ... @@ -555,11 +555,11 @@ This code uses a few more Mountebank features than the `hello` service you cre ... ``` -The `key` property uses a regular expression to parse the incoming path. In this case, you're taking the `id` that comes after `customers/` in the URL. +`key` 属性使用了一个正则表达式来解析传入的路径。在这个例子中,你将得到 URL 中 `customers/` 之后的 `id`。 -The `fromDataSource` property points to the file you're using to store your test data. +`fromDataSource` 属性指向了你储存测试数据的文件。 -The `into` property injects the result into a variable `${row}`. That variable is referenced in the following `body` section: +`into` 属性将结果注入了一个变量 `${row}`。这个变量在接下来的 `body` 部分中被引用: ```js ... @@ -573,17 +573,17 @@ The `into` property injects the result into a variable `${row}`. That variabl ... ``` -The row variable is used to populate the body of the response. In this case, it's a JSON string with the customer data. +这个 row 变量用于填充响应的 body。在这个例子中,它是一个包含客户数据的 JSON 字符串。 -Save and exit the file. +保存并关闭文件。 -Next, open `index.js` to add the new service mock to your initialization function: +接着,打开 `index.js` 将新的服务 mock 添加进初始化函数: ```bash nano src/index.js ``` -Add the highlighted line: +添加以下高亮行: ```js const mb = require('mountebank'); @@ -605,15 +605,15 @@ mbServerInstance.then(function() { }); ``` -Save and exit the file. +保存并关闭文件。 -Now start Mountebank with `npm start`. This will hide the prompt, so open up another terminal window. Test your service by sending a `GET` request to `localhost:5002/customers/3`. This will look up the customer information under `id` `3`. +现在使用 `npm start` 启动 Mountebank. 这将隐藏提示符,因此打开另一个终端窗口。通过发送 `GET` 请求至 `localhost:5002/customers/3` 来测试你的服务。这将查找 `id` = `3` 的用户信息。 ```bash curl localhost:5002/customers/3 ``` -You will see the following response: +你将会看到以下响应: ```json Output{ @@ -623,13 +623,13 @@ Output{ } ``` -In this step, you created a mock service that read data from a CSV file and returned it as a JSON response. From here, you can continue to build more complex mocks that match the services you need to test. +此步骤中,你创建了一个 mock 服务,它从 CSV 文件中读取数据并且返回一个 JSON 响应。从这里开始,您可以配合你需要测试的服务,继续构建更复杂的 mock。 -## Conclusion +## 总结 -In this article you created your own service-mocking application using Mountebank and Node.js. Now you can build mock services and share them with your team. Whether it's a complex scenario involving a vendor service you need to test around or a simple mock while you wait for another team to finish their work, you can keep your team moving by creating mock services. +在本文中,你用 Mountebank 和 Node.js 创建了自己的服务 mock 程序。现在,你可以构建 mock 服务并与团队共享。无论是涉及供应商的服务的复杂情景,还是需要在等待其他团队完成工作时进行一个简单的 mock,你都可以通过创建 mock 服务来保持团队任务的推进。 -If you want to learn more about Mountebank, check out their [documentation](http://www.mbtest.org/). If you'd like to containerize this application, check out [Containerizing a Node.js Application for Development With Docker Compose](https://www.digitalocean.com/community/tutorials/containerizing-a-node-js-application-for-development-with-docker-compose). If you'd like to run this application in a production-like environment, check out [How To Set Up a Node.js Application for Production on Ubuntu 18.04](https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-18-04). +如果你想了解更多 Mountebank 的信息,请查看他们的[文档](http://www.mbtest.org/)。如果你想将这个程序容器化,请查看[使用 Docker Compose 容器化 Node.js 开发环境](https://www.digitalocean.com/community/tutorials/containerizing-a-node-js-application-for-development-with-docker-compose)。如果你想在类生产环境中运行此程序,请查看[如何在 Ubuntu 18.04 上设置 Node.js 生产环境]((https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-18-04))。 > 如果发现译文存在错误或其他需要改进的地方,欢迎到 [掘金翻译计划](https://github.com/xitu/gold-miner) 对译文进行修改并 PR,也可获得相应奖励积分。文章开头的 **本文永久链接** 即为本文在 GitHub 上的 MarkDown 链接。 From 1ad510f8a0f140b02d8d86884aee4a0d7dc53e55 Mon Sep 17 00:00:00 2001 From: Pingren Nie Date: Mon, 16 Sep 2019 14:42:28 +0800 Subject: [PATCH 02/10] typo --- TODO1/how-to-mock-services-using-mountebank-and-node-js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md index 48ee7cfcc57..acf032f0ef1 100644 --- a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md +++ b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md @@ -349,7 +349,7 @@ module.exports = { addService }; 这个 stub 有两个部分。断言部分查找一个根(`/`)URL 的 `GET` 请求。这意味着这个 `stub` 将在有人向 mock 服务的根 URL 发送 `GET` 请求时返回响应。stub 的第二部分是 `responses` 数组。在这个例子中,将会返回一个 HTTP 状态码为`200` 的 JSON 结果。 -最后一步定义了含有这个 stub 的 imposter。 +最后一步定义了含有这个 stub 的 imposter: ```js ... From a9e926f3345f4ab8ec7cb4cec627ce06d557f129 Mon Sep 17 00:00:00 2001 From: Pingren Nie Date: Mon, 16 Sep 2019 14:49:50 +0800 Subject: [PATCH 03/10] typo 2 --- TODO1/how-to-mock-services-using-mountebank-and-node-js.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md index acf032f0ef1..4f986e33e47 100644 --- a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md +++ b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md @@ -133,7 +133,7 @@ module.exports = { ### 第 3 步 —— 构建初始化脚本 -此步骤中,你将创建一个文件来启动 Mountebank 实例。这个文件将会成为程序的入口,即当你运行程序时,这个脚本将会首先运行。当你构建新的服务 mock时,你将向此文件添加更多内容。 +此步骤中,你将创建一个文件来启动 Mountebank 实例。这个文件将会成为程序的入口,即当你运行程序时,这个脚本将会首先运行。当你构建新的服务 mock 时,你将向此文件添加更多内容。 在 `src` 目录中,创建名为 `index.js` 的文件并在你的文本编辑器中打开它: @@ -158,7 +158,7 @@ const mbServerInstance = mb.create({ 这段代码做了三件事。首先,它导入了你之前安装的 Mountebank npm 软件包(`const mb = require('mountebank');`)。接着,它导入了你在上一步创建的配置模块(`const settings = require('./settings');`)。最后,它使用 `mb.create()` 创建了一个 Moutebank 服务器实例。 -这个服务器将会监听配置文件中指定的端口,文件中的 `pidfile`,`logfile` 和 `protofile` 参数用于 Mountebank 内部记录它的进程 ID,指定日志位置,和设置加载自定义协议实现的文件。`ipWhitelist` 设置指定了允许与Moutebank 服务器通信的 IP 地址。在这个例子中,你将允许所有 IP 地址。 +这个服务器将会监听配置文件中指定的端口,文件中的 `pidfile`,`logfile` 和 `protofile` 参数用于 Mountebank 内部记录它的进程 ID,指定日志位置,和设置加载自定义协议实现的文件。`ipWhitelist` 设置指定了允许与 Moutebank 服务器通信的 IP 地址。在这个例子中,你将允许所有 IP 地址。 保存并关闭文件。 @@ -530,7 +530,7 @@ module.exports = { addService }; 这段代码定义了一个服务 mock,它寻找 URL 格式为`customers/`的 `GET` 请求。当收到一个请求时,它将在 URL 上查询顾客的 `id`,并从 CSV 文件里返回相应的记录。 -相比上一步创建的 `hello` 服务,这段代码使用了更多的 Mountebank 功能。首先,它用了 Mountebank的 [*behaviors*](http://www.mbtest.org/docs/mentalModel)。行为是一种为 stub 添加功能的方式。这个例子中,你正在使用 `lookup` 行为来查询 CSV 文件中的一条记录: +相比上一步创建的 `hello` 服务,这段代码使用了更多的 Mountebank 功能。首先,它用了 Mountebank 的 [*behaviors*](http://www.mbtest.org/docs/mentalModel)。行为是一种为 stub 添加功能的方式。这个例子中,你正在使用 `lookup` 行为来查询 CSV 文件中的一条记录: ```js ... From 260ad57587e92dcb01783a2980fad76589dd6bcc Mon Sep 17 00:00:00 2001 From: Pingren Nie Date: Fri, 18 Oct 2019 11:20:12 +0800 Subject: [PATCH 04/10] Update how-to-mock-services-using-mountebank-and-node-js.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根据校对修改,@imononoke --- ...ow-to-mock-services-using-mountebank-and-node-js.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md index 4f986e33e47..e5da364b653 100644 --- a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md +++ b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md @@ -3,7 +3,7 @@ > * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) > * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO1/how-to-mock-services-using-mountebank-and-node-js.md](https://github.com/xitu/gold-miner/blob/master/TODO1/how-to-mock-services-using-mountebank-and-node-js.md) > * 译者:[Pingren](https://github.com/Pingren) -> * 校对者 +> * 校对者:[Mononoke](https://github.com/imononoke) # 如何使用 Mountebank 和 Node.js 来 Mock 服务 @@ -11,7 +11,7 @@ 复杂的[面向服务架构(SOA)](https://en.wikipedia.org/wiki/Service-oriented_architecture)程序中,通常需要调用多个服务来运行一个完整的工作流。尽管一切服务就绪时没有问题,但如果你的代码依赖一个正在开发的服务,你不得不等待其它团队完成任务之后才能开始工作。此外,你可能需要使用外部供应商的服务,比如天气 API 或者记录系统。供应商通常不会提供足够的环境供你使用,控制他们系统的测试数据也不容易。面对这些未完成的和没有控制权的服务,代码测试让人感到沮丧。 -解决这些问题的办法是创建一个 **服务 mock**。服务 mock 用于模拟最终产品中提供的服务,但相对真正的服务而言更加轻量、简单且易于控制。你可以设置 mock 服务的响应的默认值,或者设置返回特定的测试数据,然后就可以运行你想要测试的程序,因为其依赖的服务已经就绪。如此一来,灵活的 mock 服务使你的工作流更加迅速高效。 +解决这些问题的办法是创建一个 **服务 mock**。服务 mock 用于模拟最终产品中提供的服务,但相对真正的服务而言更加轻量、简单且易于控制。你可以设置 mock 服务的响应的默认值,或者设置返回特定的测试数据,然后就可以运行你想要测试的程序,就像所依赖的服务已经就绪了一样。如此一来,灵活的 mock 服务使你的工作流更加迅速高效。 在企业环境中,创建 mock 服务有时候也叫服务虚拟化。服务虚拟化通常与昂贵的企业级工具有关,但你并不需要昂贵的工具来 mock 服务。[Mountebank](http://www.mbtest.org/)是一个免费并开源的服务 mock 工具。你可以用它 mock HTTP 服务,包括 [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) 和 [SOAP](https://en.wikipedia.org/wiki/SOAP) 服务。你还可以用它 mock [SMTP](https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol) 或 [TCP](https://en.wikipedia.org/wiki/Transmission_Control_Protocol) 请求。 @@ -70,7 +70,7 @@ npm install -save mountebank nano package.json ``` -定位到 `"scripts"` 部分并添加 `"start": "node src/index.js"`。 +定位到 "scripts" 部分并添加一个启动应用的 start 命令:"start": "node src/index.js"。 你的 `package.json` 文件应该和它类似,这取决于你如何填写初始的命令提示: @@ -127,7 +127,7 @@ module.exports = { } ``` -这个配置文件有三个条目:`port: 5000` 将端口 `5000` 分配给主 Mountebank 实例,`hello_service_port: 5001` 将端口 `5001` 分配给你将创建的 Hello World 测试服务,`customer_service_port: 5002` 将端口 `5002` 分配给使用 CSV 响应的 mock 服务。如果这些端口被占用,请根据需要修改它们。`module.exports =` 使你的其它文件可以导入这些配置。 +这个配置文件有三个条目:`port: 5000` 将端口 `5000` 分配给主 Mountebank 实例,`hello_service_port: 5001` 将端口 `5001` 分配给你将创建的 Hello World 测试服务,`customer_service_port: 5002` 将端口 `5002` 分配给使用 CSV 响应的 mock 服务。如果这些端口被占用,请根据需要修改它们。其它文件可以通过 `module.exports =` 来导入这些配置。 此步骤中,你用了 `settings.js` 来定义 Mountebank 和两个 mock 服务将会监听的端口,并使其可用于你程序的其它部分。接下来,你将会使用这些配置来构建初始化脚本,启动 Mountebank。 @@ -247,7 +247,7 @@ function postImposter(body) { module.exports = { postImposter }; ``` -这段代码首先导入了 `node-fetch` 库和你的配置文件。接着这个模块公开声明了一个函数 `postImposter`向 Mountebank 发送 post 请求。接着,`body:` 决定了这个函数将会使用 `JSON.stringify(body)`,一个 JavaScript 对象。这是你将向 Mountebank 服务 `POST` 的对象。由于这个方法在本地运行,你的请求将会发送至 `127.0.0.1`(`localhost`)。fetch 方法使用了参数中的对象并向 `url` 发送 `POST` 请求。 +这段代码首先导入了 `node-fetch` 库和你的配置文件。接着这个模块公开声明了一个函数 `postImposter`向 Mountebank 发送 post 请求。接着,`body:` 表示这个方法会使用一个 JavaScript 对象:`JSON.stringify(body)`。这是你将向 Mountebank 服务 `POST` 的对象。由于这个方法在本地运行,你的请求将会发送至 `127.0.0.1`(`localhost`)。fetch 方法使用了参数中的对象并向 `url` 发送 `POST` 请求。 此步骤中,你创建了一个 Mountebank 客户端,它向 Mountebank 服务器提交新的 mock 服务。在下一步中,你将使用这个客户端创建你的第一个 mock 服务。 From 1d33f3ad3a3f9b60328ce01a67427f8c986d6166 Mon Sep 17 00:00:00 2001 From: Pingren Nie Date: Fri, 18 Oct 2019 11:23:20 +0800 Subject: [PATCH 05/10] typo 3 --- TODO1/how-to-mock-services-using-mountebank-and-node-js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md index e5da364b653..47356bb4713 100644 --- a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md +++ b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md @@ -70,7 +70,7 @@ npm install -save mountebank nano package.json ``` -定位到 "scripts" 部分并添加一个启动应用的 start 命令:"start": "node src/index.js"。 +定位到 `"scripts"` 部分并添加一个启动应用的 `start` 命令:`"start": "node src/index.js"`。 你的 `package.json` 文件应该和它类似,这取决于你如何填写初始的命令提示: From 7425765a6877788d467e1f616058eaf4778d9873 Mon Sep 17 00:00:00 2001 From: Pingren Nie Date: Mon, 21 Oct 2019 11:27:39 +0800 Subject: [PATCH 06/10] Update how-to-mock-services-using-mountebank-and-node-js.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 根据校对修改,@TiaossuP --- ...o-mock-services-using-mountebank-and-node-js.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md index 47356bb4713..b8b27bde75a 100644 --- a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md +++ b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md @@ -9,20 +9,20 @@ ## 前言 -复杂的[面向服务架构(SOA)](https://en.wikipedia.org/wiki/Service-oriented_architecture)程序中,通常需要调用多个服务来运行一个完整的工作流。尽管一切服务就绪时没有问题,但如果你的代码依赖一个正在开发的服务,你不得不等待其它团队完成任务之后才能开始工作。此外,你可能需要使用外部供应商的服务,比如天气 API 或者记录系统。供应商通常不会提供足够的环境供你使用,控制他们系统的测试数据也不容易。面对这些未完成的和没有控制权的服务,代码测试让人感到沮丧。 +复杂的[面向服务架构(SOA)](https://en.wikipedia.org/wiki/Service-oriented_architecture)程序中,通常需要调用多个服务来运行一个完整的工作流。尽管一切服务就绪时没有问题,但如果你的代码依赖一个正在开发的服务,你就不得不等待其它团队完成任务之后才能开始工作。此外,你可能需要使用外部供应商的服务,比如天气 API 或者记录系统。供应商通常不会提供足够的环境供你使用,控制他们系统的测试数据也不容易。面对这些未完成的和没有控制权的服务,代码测试让人感到沮丧。 解决这些问题的办法是创建一个 **服务 mock**。服务 mock 用于模拟最终产品中提供的服务,但相对真正的服务而言更加轻量、简单且易于控制。你可以设置 mock 服务的响应的默认值,或者设置返回特定的测试数据,然后就可以运行你想要测试的程序,就像所依赖的服务已经就绪了一样。如此一来,灵活的 mock 服务使你的工作流更加迅速高效。 在企业环境中,创建 mock 服务有时候也叫服务虚拟化。服务虚拟化通常与昂贵的企业级工具有关,但你并不需要昂贵的工具来 mock 服务。[Mountebank](http://www.mbtest.org/)是一个免费并开源的服务 mock 工具。你可以用它 mock HTTP 服务,包括 [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) 和 [SOAP](https://en.wikipedia.org/wiki/SOAP) 服务。你还可以用它 mock [SMTP](https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol) 或 [TCP](https://en.wikipedia.org/wiki/Transmission_Control_Protocol) 请求。 -在本教程中,你将使用 [Node.js](https://nodejs.org/en/about/) 和 Mountebank 搭建两个灵活的服务 mock 程序。它们都将监听一个特定的端口的 HTTP REST 请求。除了这个简单的 mock 行为之外,这个服务还将从 [*逗号分隔值* (CSV) 文件](https://en.wikipedia.org/wiki/Comma-separated_values)中获取 mock 数据。完成教程后,你将能 mock 各种各样的服务行为,更轻松地开发和测试程序。 + 在本教程中,你将使用 [Node.js](https://nodejs.org/en/about/) 和 Mountebank 搭建两个灵活的服务 mock 程序。它们都将监听一个特定的端口的 HTTP REST 请求。除了这个简单的 mock 行为之外,这个服务还将从 [**逗号分隔值** (CSV) 文件](https://en.wikipedia.org/wiki/Comma-separated_values)中获取 mock 数据。完成教程后,你将能 mock 各种各样的服务行为,更轻松地开发和测试程序。 ### 前提 为了学习本教程,你需要: - 在你的机器上安装 8.10.0 及以上版本的 Node.js。本教程将使用 8.10.0 版。要安装 Node.js,请查看[如何在 Ubuntu 18.04 上安装 Node.js](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-18-04) 或 [如何在 macOS 上安装 Node.js 和创建本地开发环境](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-and-create-a-local-development-environment-on-macos)。 -- 发送 HTTP 请求的工具,比如 [cURL](https://curl.haxx.se/) 或 [Postman](https://www.getpostman.com/)。本教程将使用 cURL,因为它默认安装在大多数机器上;如果你的机器上没有 cURL, 请看看这个[安装文档](https://curl.haxx.se/docs/install.html)。 +- 发送 HTTP 请求的工具,比如 [cURL](https://curl.haxx.se/) 或 [Postman](https://www.getpostman.com/)。本教程将使用 cURL,因为大多数机器上都默认安装了它;如果你的机器上没有 cURL, 请看看这个[安装文档](https://curl.haxx.se/docs/install.html)。 ### 第 1 步 —— 启动 Node.js 程序 @@ -30,7 +30,7 @@ 请注意:你可以使用命令 `npm install -g mountebank` 全局安装 Mountebank ,将其作为独立的应用程序。然后你可以使用 `mb` 命令运行它,并用 REST 请求添加 mock。 -虽然这是运行 Mountebank 的最快方法,自行构建的 Mountebank 程序可以在启动时运行一系列的预定义 mock,而这些又可以被保存在源码管理中并与团队共享。本教程将会手动构建有这种优点的 Mountebank 程序。 +虽然这是运行 Mountebank 的最快方法,但是自己搭建的 Mountebank 程序可以在启动时运行一系列的预定义 mock,而这些又可以被保存在源码管理中并与团队共享。本教程将会手动构建有这种优点的 Mountebank 程序。 首先,创建一个新目录保存你的程序。你可以根据需要命名,但在本教程中我们将它命名为 `app`: @@ -91,7 +91,7 @@ nano package.json } ``` -通过创建你的程序,安装 Mountebank 并添加一个启动脚本,你现在拥有了 Mountebank 程序的基础。接下来,你将添加一个配置文件,来保存程序特定的设置。 +通过创建你的程序、安装 Mountebank、添加一个启动脚本,你现在拥有了 Mountebank 程序的基础。接下来,你将添加一个配置文件,来保存程序特定的设置。 ### 第 2 步 —— 创建配置文件 @@ -158,7 +158,7 @@ const mbServerInstance = mb.create({ 这段代码做了三件事。首先,它导入了你之前安装的 Mountebank npm 软件包(`const mb = require('mountebank');`)。接着,它导入了你在上一步创建的配置模块(`const settings = require('./settings');`)。最后,它使用 `mb.create()` 创建了一个 Moutebank 服务器实例。 -这个服务器将会监听配置文件中指定的端口,文件中的 `pidfile`,`logfile` 和 `protofile` 参数用于 Mountebank 内部记录它的进程 ID,指定日志位置,和设置加载自定义协议实现的文件。`ipWhitelist` 设置指定了允许与 Moutebank 服务器通信的 IP 地址。在这个例子中,你将允许所有 IP 地址。 +这个服务器将会监听配置文件中指定的端口,文件中的 `pidfile`,`logfile` 和 `protofile` 参数用于 Mountebank 内部记录它的进程 ID、指定日志位置、设置加载自定义协议实现的文件。`ipWhitelist` 设置指定了允许与 Moutebank 服务器通信的 IP 地址。在这个例子中,你将允许所有 IP 地址。 保存并关闭文件。 @@ -255,7 +255,7 @@ module.exports = { postImposter }; 在之前的步骤中,你构建了一个程序,创建 Mountebank 服务器并编写了请求服务器的代码。现在是时候使用这些代码来构建一个 imposter,即 mock 服务。 -在 Mountebank 中,每个 imposter 包含了 [*stubs*](http://www.mbtest.org/docs/mentalModel)。Stubs 是决定 imposter 返回内容的配置集合。Stubs 可以被细分成多个 [*断言*和*响应*](http://www.mbtest.org/docs/mentalModel) 的组合。断言是触发 imposter 响应的规则。断言可以使用许多不同类型的信息,包括了 URL,请求内容(使用 XML 或 JSON),以及 HTTP 方法。 +在 Mountebank 中,每个 imposter 包含了 [*stubs*](http://www.mbtest.org/docs/mentalModel)。Stubs 是决定 imposter 返回内容的配置集合。Stubs 可以被细分成多个 [**断言**和**响应**](http://www.mbtest.org/docs/mentalModel) 的组合。断言是触发 imposter 响应的规则。断言可以使用许多不同类型的信息,包括了 URL,请求内容(使用 XML 或 JSON),以及 HTTP 方法。 从[模型 - 视图 - 控制器(MVC)](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller)程序的角度来看,imposter 像控制器而 stubs 像控制器中的行为。断言则是指向特定控制器的行为的路由规则。 From 5326e156ddc0db768c9cede0677f29534167bfd8 Mon Sep 17 00:00:00 2001 From: Pingren Nie Date: Mon, 21 Oct 2019 11:29:50 +0800 Subject: [PATCH 07/10] add proofreader info --- TODO1/how-to-mock-services-using-mountebank-and-node-js.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md index b8b27bde75a..17629feb42c 100644 --- a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md +++ b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md @@ -3,7 +3,7 @@ > * 译文出自:[掘金翻译计划](https://github.com/xitu/gold-miner) > * 本文永久链接:[https://github.com/xitu/gold-miner/blob/master/TODO1/how-to-mock-services-using-mountebank-and-node-js.md](https://github.com/xitu/gold-miner/blob/master/TODO1/how-to-mock-services-using-mountebank-and-node-js.md) > * 译者:[Pingren](https://github.com/Pingren) -> * 校对者:[Mononoke](https://github.com/imononoke) +> * 校对者:[Mononoke](https://github.com/imononoke),[TiaossuP](https://github.com/TiaossuP) # 如何使用 Mountebank 和 Node.js 来 Mock 服务 From 1d13732592bced5b3080efeeb171dcb3ea5ab5c0 Mon Sep 17 00:00:00 2001 From: lsvih Date: Mon, 21 Oct 2019 14:16:35 +0800 Subject: [PATCH 08/10] reformat --- .../how-to-mock-services-using-mountebank-and-node-js.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md index 17629feb42c..c6042e59b48 100644 --- a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md +++ b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md @@ -15,14 +15,14 @@ 在企业环境中,创建 mock 服务有时候也叫服务虚拟化。服务虚拟化通常与昂贵的企业级工具有关,但你并不需要昂贵的工具来 mock 服务。[Mountebank](http://www.mbtest.org/)是一个免费并开源的服务 mock 工具。你可以用它 mock HTTP 服务,包括 [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) 和 [SOAP](https://en.wikipedia.org/wiki/SOAP) 服务。你还可以用它 mock [SMTP](https://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol) 或 [TCP](https://en.wikipedia.org/wiki/Transmission_Control_Protocol) 请求。 - 在本教程中,你将使用 [Node.js](https://nodejs.org/en/about/) 和 Mountebank 搭建两个灵活的服务 mock 程序。它们都将监听一个特定的端口的 HTTP REST 请求。除了这个简单的 mock 行为之外,这个服务还将从 [**逗号分隔值** (CSV) 文件](https://en.wikipedia.org/wiki/Comma-separated_values)中获取 mock 数据。完成教程后,你将能 mock 各种各样的服务行为,更轻松地开发和测试程序。 +在本教程中,你将使用 [Node.js](https://nodejs.org/en/about/) 和 Mountebank 搭建两个灵活的服务 mock 程序。它们都将监听一个特定的端口的 HTTP REST 请求。除了这个简单的 mock 行为之外,这个服务还将从 [**逗号分隔值** (CSV) 文件](https://en.wikipedia.org/wiki/Comma-separated_values)中获取 mock 数据。完成教程后,你将能 mock 各种各样的服务行为,更轻松地开发和测试程序。 ### 前提 为了学习本教程,你需要: - 在你的机器上安装 8.10.0 及以上版本的 Node.js。本教程将使用 8.10.0 版。要安装 Node.js,请查看[如何在 Ubuntu 18.04 上安装 Node.js](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-18-04) 或 [如何在 macOS 上安装 Node.js 和创建本地开发环境](https://www.digitalocean.com/community/tutorials/how-to-install-node-js-and-create-a-local-development-environment-on-macos)。 -- 发送 HTTP 请求的工具,比如 [cURL](https://curl.haxx.se/) 或 [Postman](https://www.getpostman.com/)。本教程将使用 cURL,因为大多数机器上都默认安装了它;如果你的机器上没有 cURL, 请看看这个[安装文档](https://curl.haxx.se/docs/install.html)。 +- 发送 HTTP 请求的工具,比如 [cURL](https://curl.haxx.se/) 或 [Postman](https://www.getpostman.com/)。本教程将使用 cURL,因为大多数机器上都默认安装了它;如果你的机器上没有 cURL,请看看这个[安装文档](https://curl.haxx.se/docs/install.html)。 ### 第 1 步 —— 启动 Node.js 程序 @@ -528,7 +528,7 @@ function addService() { module.exports = { addService }; ``` -这段代码定义了一个服务 mock,它寻找 URL 格式为`customers/`的 `GET` 请求。当收到一个请求时,它将在 URL 上查询顾客的 `id`,并从 CSV 文件里返回相应的记录。 +这段代码定义了一个服务 mock,它寻找 URL 格式为 `customers/` 的 `GET` 请求。当收到一个请求时,它将在 URL 上查询顾客的 `id`,并从 CSV 文件里返回相应的记录。 相比上一步创建的 `hello` 服务,这段代码使用了更多的 Mountebank 功能。首先,它用了 Mountebank 的 [*behaviors*](http://www.mbtest.org/docs/mentalModel)。行为是一种为 stub 添加功能的方式。这个例子中,你正在使用 `lookup` 行为来查询 CSV 文件中的一条记录: @@ -607,7 +607,7 @@ mbServerInstance.then(function() { 保存并关闭文件。 -现在使用 `npm start` 启动 Mountebank. 这将隐藏提示符,因此打开另一个终端窗口。通过发送 `GET` 请求至 `localhost:5002/customers/3` 来测试你的服务。这将查找 `id` = `3` 的用户信息。 +现在使用 `npm start` 启动 Mountebank。这将隐藏提示符,因此打开另一个终端窗口。通过发送 `GET` 请求至 `localhost:5002/customers/3` 来测试你的服务。这将查找 `id=3` 的用户信息。 ```bash curl localhost:5002/customers/3 From 53661d14e370f3adf2816d2d2543b2b09fc7e018 Mon Sep 17 00:00:00 2001 From: Pingren Nie Date: Mon, 21 Oct 2019 14:38:27 +0800 Subject: [PATCH 09/10] Update how-to-mock-services-using-mountebank-and-node-js.md --- TODO1/how-to-mock-services-using-mountebank-and-node-js.md | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md index c6042e59b48..6cb2b99792a 100644 --- a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md +++ b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md @@ -428,6 +428,7 @@ Output{"message": "hello world"} 此步骤中,你创建了你的第一个 mock 服务。这是一个对 `GET` 请求响应 `hello world` 测试 mock。这个 mock 主要是为了展示而创建的;实际上,即使不用它,你通过构建一个小型 Express 程序能达到同样的结果。下一步中,你将创建一个更加复杂的 mock,它利用了一些 Mountebank 的功能。 ### Step 6 --- Building a Data-Backed Mock Service +### 第 6 步 —— 构建一个数据驱动的 mock 服务 尽管你在上一步创建的服务类型足以应付一些场景,绝大多数的测试需要更复杂的响应。此步骤中,你将创建一个服务,它使用 URL 中的参数查询 CSV 文件中的一条记录。 From 8b37629ced872873baaf5d01d8860d18f34629f8 Mon Sep 17 00:00:00 2001 From: Pingren Nie Date: Mon, 21 Oct 2019 14:39:43 +0800 Subject: [PATCH 10/10] Update how-to-mock-services-using-mountebank-and-node-js.md --- TODO1/how-to-mock-services-using-mountebank-and-node-js.md | 1 - 1 file changed, 1 deletion(-) diff --git a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md index 6cb2b99792a..54b86b1c1f0 100644 --- a/TODO1/how-to-mock-services-using-mountebank-and-node-js.md +++ b/TODO1/how-to-mock-services-using-mountebank-and-node-js.md @@ -427,7 +427,6 @@ Output{"message": "hello world"} 此步骤中,你创建了你的第一个 mock 服务。这是一个对 `GET` 请求响应 `hello world` 测试 mock。这个 mock 主要是为了展示而创建的;实际上,即使不用它,你通过构建一个小型 Express 程序能达到同样的结果。下一步中,你将创建一个更加复杂的 mock,它利用了一些 Mountebank 的功能。 -### Step 6 --- Building a Data-Backed Mock Service ### 第 6 步 —— 构建一个数据驱动的 mock 服务 尽管你在上一步创建的服务类型足以应付一些场景,绝大多数的测试需要更复杂的响应。此步骤中,你将创建一个服务,它使用 URL 中的参数查询 CSV 文件中的一条记录。