Skip to Content
学习测试驱动开发
book

学习测试驱动开发

by Saleem Siddiqui
July 2025
Intermediate to advanced
280 pages
3h 5m
Chinese
O'Reilly Media, Inc.
Content preview from 学习测试驱动开发

第 6 章 JavaScript 中的模块 JavaScript 中的模块

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

模块是一个函数或对象,它提供了一个接口,但隐藏了其状态和实现。

Douglas Crockford,《JavaScript:好的部分》(O'Reilly,2008 年)

在本章中,我们将采取一些措施来清理和改进 JavaScript 代码。我们将使用 JavaScript 模块将测试代码与生产代码分开。在 JavaScript 中编写模块有几种方法,我们将介绍四种不同的风格及其在代码中的适用性。我们将关注测试代码的组织方式,然后改进其运行方式和输出结果。最后,我们将删除测试中的一些冗余代码。这些工作都很繁重,让我们开始吧!

将代码分成模块

让我们将Money 和Portfolio 类与测试代码分开。 我们在test_money.js 的同一文件夹中创建两个名为money.js 和portfolio.js 的新文件,并将相关代码移至此处。这就是我们的新文件夹结构:

js
├── money.js
├── portfolio.js
└── test_money.js

这就是portfolio.js 的样子:

class Portfolio {
    constructor() {
        this.moneys = [];
    }

    add(...moneys) {
        this.moneys = this.moneys.concat(moneys);
    }

    evaluate(currency) {
        let total = this.moneys.reduce((sum, money) => {
            return sum + money.amount;
        }, 0);
        return new Money(total, currency);
    }
}

此处未显示的文件money.js 同样包含Money 类及其方法。

现在,当我们在TDD_Project_Root 文件夹中执行node js/test_money.js 来运行测试时,就会得到我们的老朋友ReferenceError :

ReferenceError: Money is not defined

现在,类Money 和Portfolio 都在自己的文件中,测试代码再也无法访问它们了。怎么办?

我们从测试代码中得到提示:使用require 语句访问assert 库。我们能require Money 和Portfolio 吗?

可以!不过,在此之前,我们首先要从各自的文件中导出这些类。

在money.js 的末尾,添加一行导出Money 类的语句:

module.exports = Money;

同样,我们在portfolio.js 文件末尾添加一条module.exports 语句:

module.exports = Portfolio;

现在,在test_money.js 的顶部添加两条require 语句:

const Money = require('./money');
const Portfolio = require('./portfolio');

现在运行测试时会发生什么?我们又得到了ReferenceError :

.../portfolio.js:14
        return new Money(total, currency);
        ^

ReferenceError: Money is not defined

等等:现在portfolio.js ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

AI原生软件交付

AI原生软件交付

Nick Durkin, Eric Minick, Chinmay Gaikwad
生成式人工智能可视化

生成式人工智能可视化

Priyanka Vergadia, Valliappa Lakshmanan

Publisher Resources

ISBN: 9798341663183