javascript测试

Today, we are going to discuss testing in JavaScript and help you start your journey towards understanding and mastering it.

今天,我们将讨论JavaScript测试,并帮助您开始理解和掌握它的旅程。

Testing is one of the most important topics in software development, but a lot of developers still shy away from it. This article is here to change that.

测试是软件开发中最重要的主题之一,但是许多开发人员仍然回避它。 本文是为了改变这一点

The primary motivation behind this article is to give a clear overview of the entire world of JavaScript testing and making it simple to understand. Even if you have no prior experience in testing, this article will prove to be the perfect start for your journey.

本文的主要动机是为了清晰地概述JavaScript测试的整个世界,并使之易于理解。 即使您没有测试经验,这篇文章也将被证明是您旅程的完美起点。

So, without wasting any further time, let’s get started.

因此,在不浪费更多时间的情况下,让我们开始吧。

为什么测试很重要 (Why testing is important)

Before diving into the various types and concepts of software testing, you should first have a clear understanding of why you should actually care about automated testing in the first place.

在深入探讨软件测试的各种类型和概念之前,您应该首先清楚地了解为什么首先应该真正关心自动化测试。

建立对代码的信心: (Building Confidence In Your Code:)

To know that your code is working as planned, it needs to be tested in some kind. Manual test work for most small applications but don’t provide the security and confidence level you get using automated tests.

要知道您的代码是否按计划工作,需要进行某种形式的测试。 手动测试适用于大多数小型应用程序,但不能提供使用自动测试获得的安全性和置信度。

Automated tests make it easy to test almost every possible situation and allow you to run them whenever you are making a change to your code.

自动化测试可以轻松测试几乎所有可能的情况,并允许您在更改代码时运行它们。

Identifying every success and failure case and writing tests for them will ensure that you are confident with the code you are deploying for production.

确定每个成功和失败的案例并为它们编写测试,将确保您对部署到生产中的代码充满信心。

编写最小代码: (Writing Minimal Code:)

Testing also helps you to reduce the amount of code you are writing for a particular feature. After testing, your main goal is to write the minimal required code to make the tests pass. This coding style where you write tests before you write the actual implementation is also known as TDD (Test-driven development).

测试还可以帮助您减少为特定功能编写的代码量。 测试之后,您的主要目标是编写所需的最少代码以使测试通过。 在编写实际实现之前编写测试的这种编码方式也称为TDD(测试驱动的开发)。

After successfully testing, you can focus on writing clean implementations with as minimal code as possible.

成功测试之后,您可以专注于用最少的代码编写干净的实现。

摆脱回归错误: (Getting Rid Of Regression Bugs:)

Do you know the feeling when you have just finished a new feature of your application and want to release it to production and all of a sudden, an old feature isn’t working anymore? You are absolutely clueless about why this is happening and will probably waste a lot of time searching for the issue.

当您刚刚完成应用程序的新功能并想将其发布到生产环境中时,突然之间,旧功能不再起作用了,您是否知道这种感觉? 您绝对不知道为什么会这样,并且可能会浪费大量时间来寻找问题。

This situation would have never occurred if you had tested your old features. You could have frequently run those tests to check if your application is still working as expected. The tests would also give you a better idea of what exactly isn’t working anymore because the appropriate test cases would fail.

如果您已经测试了旧功能,则永远不会发生这种情况。 您可能经常运行这些测试,以检查您的应用程序是否仍按预期运行。 这些测试还将使您更好地了解什么不再起作用,因为适当的测试用例将失败。

测试类型 (Types of tests)

There are a few different types of tests, and it is essential to know how they differ from each other. Most applications will require you to write multiple kinds of tests to get the best result possible.

有几种不同类型的测试,因此必须知道它们之间的区别。 大多数应用程序将要求您编写多种测试以获得最佳结果。

单元测试: (Unit tests:)

The purpose of a unit test is to validate the functionality of a relatively small piece of software, independently from other parts. Unit tests are narrow in scope, which allows us to cover all cases to ensure that every single part works correctly.

单元测试的目的是独立于其他部分,验证相对较小软件的功能。 单元测试的范围很窄,这使我们能够涵盖所有情况,以确保每个部分都能正常工作。

They are small and highly focused tests that can efficiently be executed on your local machine because of their fast execution time. You are going to have hundreds, if not thousands of these tests and run them on a regular basis while developing.

它们是小型且高度集中的测试,由于其执行时间短,因此可以在本地计算机上有效地执行。 您将拥有数百个(如果不是数千个)这些测试,并在开发时定期运行它们。

The only downside to these kinds of tests is that they are not executed on real devices and therefore have lower fidelity than the other types of tests.

这些测试的唯一缺点是它们不能在真实设备上执行,因此其保真度低于其他类型的测试。

集成测试: (Integration tests:)

Integration tests demonstrate that the different parts of your application work together in a real-life production environment. They verify that two separate modules or components are working together in the way they should.

集成测试表明,应用程序的不同部分可以在实际的生产环境中协同工作。 他们验证了两个独立的模块或组件是否按应有的方式协同工作。

These tests are of medium size and have a much higher execution time then Unit tests. They aren’t executed as often but are still vital for checking the health status of your applications. Their fidelity is also a lot higher because they run on real devices and verify the actual interaction between various components of your application.

这些测试的大小适中,执行时间比单元测试长得多。 它们执行的频率不高,但对于检查应用程序的运行状况仍然至关重要。 它们的保真度也更高,因为它们在真实设备上运行并验证应用程序各个组件之间的实际交互。

端到端测试: (End-to-End tests:)

End-to-End tests validate complex scenarios from end to end, and usually require external resources, like databases or web servers, to be present. Imagine you have an application with a sign-up flow comprising of several steps, and you want to test the entire flow, that’s where End-to-End tests come into play.

端到端测试从头到尾验证复杂的场景,并且通常需要使用外部资源,例如数据库或Web服务器。 想象一下,您的应用程序具有包含几个步骤的注册流程,并且您想要测试整个流程,这就是端到端测试的作用。

E2E tests will also run on real devices just like integration tests and therefore, will again be quite slow in their execution.

E2E测试也将像集成测试一样在真实设备上运行,因此,它们的执行速度仍然很慢。

The only downside to these kinds of tests is that debugging them and finding out what went wrong if a particular test fails becomes very hard because of their vast scope.

这些测试的唯一缺点是,由于它们的范围很广,因此调试它们并找出在特定测试失败时出了哪些问题会变得非常困难。

概念 (Concepts)

Before starting to write tests for your code, you first need to be familiar with the most crucial testing concepts and when you need to use them. These concepts will influence the architecture of your application and how you write code in general but more on that in a later section.

在开始为代码编写测试之前,您首先需要熟悉最关键的测试概念以及何时使用它们。 这些概念将影响应用程序的体系结构以及总体上编写代码的方式,但在后面的部分中会对此有更多影响。

匹配器: (Matchers:)

Matchers let you validate the results and values of your tests in different ways and are used to make sure that the results of the test match your expectations.

匹配器使您可以用不同的方式验证测试的结果和值,并用于确保测试的结果符合您的期望。

Imagine you have a function that calculates the result of a certain factorial number. Testing the function can then be done using the expect() function and a simple matcher that checks if the result of the function matches the expected value.

假设您有一个函数可以计算某个阶乘数的结果。 然后,可以使用Expect()函数和一个简单的匹配器来测试该函数,该匹配器检查函数的结果是否与期望值匹配。

test('factorial of 2', () => { expect(factorial(2)).toBe(2); });

The expect() function checks if the result meets the conditions defined by the matcher. We will make use of different matchers in the Jest testing framework at a later point in this guide.

Expect()函数检查结果是否满足匹配器定义的条件。 在本指南的后面,我们将在Jest测试框架中使用不同的匹配器。

模拟: (Mocking:)

An object under a test might have dependencies on other objects or services. To isolate the behavior of an object, you want to replace the other objects it interacts with by mocks that simulate the behavior of the real objects.

被测对象可能与其他对象或服务具有依赖性。 要隔离对象的行为,您想用模拟真实对象行为的模拟替换与之交互的其他对象。

Mocks help your tests to avoid test unreliability (flakiness) and improve the speed of your tests. They are also useful if the real objects are impractical to incorporate into tests.

模拟可以帮助您避免测试的不可靠性(易碎性)并提高测试速度。 如果实际对象不适合合并到测试中,它们也很有用。

In short, mocking is creating objects or services that simulate the behavior of real objects (A database, for example).

简而言之,模拟就是创建模拟真实对象(例如数据库)行为的对象或服务。

生命周期: (Lifecycle:)

When testing, you often execute multiple tests after each other and have some setup work that needs to happen before the tests run. Most frameworks provide helper functions to handle these scenarios.

测试时,您经常会接连执行多个测试,并且在运行测试之前需要进行一些设置工作。 大多数框架提供帮助程序功能来处理这些情况。

Here is an example of lifecycle methods in the Jest testing framework.

这是Jest测试框架中生命周期方法的示例。

beforeEach(() => {
// Initialize objects
});afterEach(() => {
// Tear down objects
});

可测试的架构 (Testable architecture)

Before starting to write tests for your code, you first need to make sure that your application’s architecture is testable. If it is not, you need to understand why not and what you can do about it.

在开始为代码编写测试之前,首先需要确保应用程序的体系结构是可测试的。 如果不是,那么您需要了解为什么不这样做以及您可以对此做些什么。

Untestable architecture is probably the most common reason why many people find testing tedious and difficult. If your code is not structured properly, you are definitely going to find it difficult to write tests for it.

无法测试的架构可能是许多人发现测试乏味且困难的最常见原因。 如果您的代码结构不正确,您肯定会发现很难为其编写测试。

Let’s explore some important concepts you should know when talking about testable architecture.

让我们探讨在谈论可测试架构时应该了解的一些重要概念。

依赖注入: (Dependency injection:)

Dependency injection is a concept where an object supplies the dependencies of another object. Instead of using the new keyword whenever creating a new object, all you need to do is ask the other object to give you the instance you want.

依赖注入是一个对象提供另一个对象的依赖关系的概念。 您只需要做的就是让另一个对象为您提供所需的实例,而不是在创建新对象时使用new关键字。

This concept helps when you need to change the implementation of some object, e.g. when you mock it for a particular test. Many modern frameworks like Angular and Nest.js have dependency injection already build in, but it is still good to know how it functions at a base level.

当您需要更改某些对象的实现时,例如,当您为特定测试模拟它时,此概念会有所帮助。 许多现代框架(例如Angular和Nest.js)已经内置了依赖项注入,但是最好知道它在基本级别上如何工作。

For more information on dependency injection, you can visit the following article.

有关依赖项注入的更多信息,您可以访问以下文章。

SRP(单一责任原则): (SRP (Single responsibility principle):)

The single responsibility principle, also known as SRP, is one of the SOLID principles and defines that a function should have a single purpose. This makes it far easier to test that each function does its part correctly.

单一责任原则,也称为SRP,是SOLID原则之一,它定义了功能应具有单一目的。 这使得测试每个功能是否正确发挥作用变得容易得多。

If your function or service is performing more than one responsibility, then it is time to identify those responsibilities and separate them into individual functions.

如果您的职能或服务执行的职责不止一项,那么该是识别这些职责并将其划分为各个职能的时候了。

避免副作用: (Avoid side effects:)

Your functions depend on external variables and services, and you have to set up that variable or service before testing your function. You’ll also have to trust that any other code being run isn’t altering that same variables and states.

您的函数取决于外部变量和服务,并且在测试功能之前必须设置该变量或服务。 您还必须相信正在运行的任何其他代码都不会更改相同的变量和状态。

That is why you should avoid writing functions that alter any external state (like writing to a file or saving values to a database). This prevents side effects and allows you to test your code with confidence.

这就是为什么您应避免编写会更改任何外部状态的函数(例如,写入文件或将值保存到数据库)的原因。 这样可以防止副作用,并使您可以放心地测试代码。

得墨meter耳定律: (Law of Demeter:)

The Law of Demeter, which is also known as the “principle of least knowledge” states that a specific unit should have limited knowledge of the other units it coordinates with. The more your code depends on the internal details of the objects it interacts with, the more difficulty you will have when writing tests for them.

得墨meter耳定律(也称为“最少知识原理”)规定,特定单位对与其协调的其他单位的了解有限。 您的代码越依赖于与之交互的对象的内部细节,在为它们编写测试时就会遇到更多的困难。

More information about the Law of Demeter can be found here.

有关Demeter定律的更多信息,可以在这里找到。

各种测试工具概述 (Overview of the different testing tools)

Now that you have an overview of the essential concepts in the testing world and when you need to use them, let’s continue by looking at a short summary of the different Javascript testing tools that are available today.

现在,您已概述了测试领域的基本概念以及何时需要使用它们,让我们继续以简短的摘要介绍一下当今可用的各种Javascript测试工具。

Note: I will not cover every tool there is but instead look at the most important once to give you a quick overview of there benefits and weaknesses.

注意:我不会介绍所有工具,而是会介绍最重要的工具,以使您快速了解那里的优点和缺点。

Jest is an open-source testing framework created by Facebook with a focus on simplicity. Jest makes it faster and easier to write JavaScript tests by having everything included out of the box and needing zero configuration. Jest also runs your tests in parallel, providing a smoother, faster test run.

Jest是Facebook创建的一个开放源代码测试框架,专注于简单性。 Jest通过开箱即用并需要零配置来使编写JavaScript测试更快,更轻松。 Jest还可以并行运行测试,从而提供更流畅,更快的测试运行。

Mocha is a flexible JavaScript testing libraries available and aims to make asynchronous testing simple and fun. It provides developers with a basic test framework and gives them the option to choose which assertion, mocking, and spy libraries they want to use.

Mocha是一个灵活JavaScript测试库,旨在使异步测试变得简单而有趣 它为开发人员提供了一个基本的测试框架,并为他们提供了选择要使用的断言,模拟和间谍库的选项。

It requires some additional setup and configuration but gives you complete control of your testing framework in return.

它需要一些额外的设置和配置,但可以让您完全控制测试框架。

Cypress is an all in one testing tool that is focused on making End-to-End testing easy and modern. Their tests are executed in the browser themself which gives them a better execution time and no network lag.

赛普拉斯是一款多功能的测试工具,致力于使端到端测试变得轻松而现代。 他们的测试在他们自己的浏览器中执行,这为他们提供了更好的执行时间并且没有网络延迟。

Cypress is used to deal with complex UI’s running on modern Javascript stacks. By using their framework and assertion library, it becomes easy to validate states in the UI. Cypress will then automatically wait for your application to reach this state before moving on.

赛普拉斯用于处理在现代Javascript堆栈上运行的复杂UI。 通过使用它们的框架和声明库,可以轻松验证UI中的状态。 然后,赛普拉斯将自动等待您的应用程序达到该状态,然后再继续。

Cypress is a newer and more modern tool than Jest and Mocha and is an excellent start for beginners and End-to-End testing in general.

与Jest和Mocha相比,赛普拉斯是一种更新,更现代的工具,对于一般的初学者和端到端测试而言,赛普拉斯是一个很好的起点。

笑话简介 (Introduction to Jest)

As already mentioned above, this guide will focus on the Jest testing framework because it is the most popular framework out there. But most of the concepts apply to all testing frameworks and can be useful no matter which technology you are using.

如前所述,本指南将重点介绍Jest测试框架,因为它是目前最流行的框架。 但是大多数概念都适用于所有测试框架,并且无论您使用哪种技术,它都可能有用。

Jest is an open-source project maintained by Facebook and is especially well suited for Unit and Integration testing. Its strengths are:

Jest是Facebook维护的一个开源项目,特别适合单元测试和集成测试。 它的优势是:

  • It is simplistic and fast

    它既简单又快速
  • It provides everything out of the box and thereby doesn’t require and configuration (though you can change the configuration if you so choose)

    它提供了开箱即用的所有内容,因此不需要配置(尽管您可以选择更改配置)
  • It can perform snapshot testing

    它可以执行快照测试

Now we will explore some practical examples so you can put your knowledge into practice.

现在,我们将探索一些实际示例,以便您将知识付诸实践。

安装 (Installation)

Jest can be installed using either npm or yarn:

Jest可以使用npm或yarn进行安装:

yarn add --dev jest 
# or
npm install --save-dev jest

Notice that this will install Jest as a dev dependency as part of your package.json file in the current project. You can alternatively install it globally if you so choose.

注意,这会将Jest作为开发依赖项安装为当前项目中package.json文件的一部分。 您也可以选择在全球范围内安装它。

yarn global add jest 
# or
npm install jest -g

You can also add this line to your package.json to run your test using the test command.

您还可以将此行添加到package.json中,以使用test命令运行测试。

{ "scripts": { "test": "jest" } }

您的第一个测试 (Your first test)

Now that we have installed Jest it is finally time to write our first test. But before we do that, we will write some basic code that we can test in the first place.

现在我们已经安装了Jest,现在终于可以编写我们的第一个测试了。 但是在执行此操作之前,我们将首先编写一些可以测试的基本代码。

For that, we will create two files so we can get going.

为此,我们将创建两个文件,以便我们开始。

touch maths.js 
touch maths.spec.js

We will use the following function for calculating a factorial number to write our first test.

我们将使用以下函数来计算阶乘数以编写第一个测试。

function factorialize(num) {if (num < 0) return -1;else if (num == 0) return 1;else {return num * factorialize(num - 1);}
}module.exports = { factorialize }

Here are some very basic test cases for this small function.

这是此小功能的一些非常基本的测试用例。

const { factorialize } = require("./maths");test("factorial of 3", () => {expect(factorialize(3)).toBe(6);
});test("factorial of 5", () => {expect(factorialize(5)).toBe(120);
});

Running the yarn test command in your terminal should give you the following output:

在终端中运行yarn test命令,将为您提供以下输出:

匹配器 (Matchers)

As already said above matchers let you validate the results and values of your tests in different ways.

如上所述,匹配器可让您以不同的方式验证测试的结果和值。

They are most commonly used to compare the result of the expect() function to the value passed as an argument to the matcher (That is also what we did above).

它们最常用于将Expect()函数的结果与作为参数传递给匹配器的值进行比较(这也是我们上面所做的)。

Here is a list of the most common matchers:

以下是最常见的匹配器列表:

  • toBe — compares for strict equality (e.g. ===)

    toBe —比较严格相等(例如===)
  • toEqual — compares the values of two variables/objects

    toEqual —比较两个变量/对象的值
  • toBeNull — checks if the value is null

    toBeNull —检查值是否为null
  • toBeDefined — checks if the value is defined

    toBeDefined-检查值是否已定义
  • toBeUndefined — checks if the value is undefined

    toBeUndefined —检查值是否未定义
  • toBeTruthy — checks if the value is true (similar to an if statement)

    toBeTruthy-检查值是否为真(类似于if语句)
  • toBeFalsy — checks if the value is false(similar to an if statement)

    toBeFalsy —检查值是否为false(类似于if语句)
  • toBeGreaterThan — checks if the result of the expect() function is greater than the argument

    toBeGreaterThan —检查Expect()函数的结果是否大于参数
  • toContain — checks if the result of expect() contains a value

    toContain —检查Expect()的结果是否包含值
  • toHaveProperty — checks if an object has a property, and optionally checks its value

    toHaveProperty —检查对象是否具有属性,并可选地检查其值
  • toBeInstanceOf — checks if an object is an instance of a class

    toBeInstanceOf —检查对象是否是类的实例

These matchers can also be negated using the not statement:

也可以使用not语句否定这些匹配器:

test("factorial of 3 is not 5", () => {     expect(factorialize(3)).not.toBe(5); 
});

You can also use additional matchers that are maintained by the Jest community.

您还可以使用Jest社区维护的其他匹配器。

设置和拆卸 (Setup and Teardown)

Often when writing tests, you will have to do some kind of setup like initializing variables before tests run and some sort of action after they have finished.

通常,在编写测试时,您将必须进行某种设置,例如在测试运行之前初始化变量,并在测试完成后进行某种操作。

Jest provides two different ways you can do that.

Jest提供了两种不同的方式来实现这一目标。

一次性设置: (One-Time Setup:)

In some cases, you only need to do the setup once, at the beginning of your test file. In that scenario, you can use the beforeAll() and afterAll() helper functions that will execute before the tests start and after all have finished.

在某些情况下,您只需在测试文件的开头执行一次设置即可。 在那种情况下,您可以使用beforeAll()afterAll()帮助程序函数,这些函数将在测试开始之前和完成之后执行。

beforeAll(() => {return initializeDatabase();
});afterAll(() => {return clearDatabase();
});test('query from database', () => {expect(database.getObject('Florida')).toBeTruthy();
});

为每个测试重复设置:(Repeating setup for each test:)

If you have a setup process that needs to run before each test than you should use the beforeEach() and afterEach() functions.

如果您有一个需要在每次测试之前运行的设置过程,则应使用beforeEach()afterEach()函数。

beforeEach(() => {initializeDatabase();
});afterEach(() => {clearDatabase();
});test('query from database', () => {expect(database.getObject('Florida')).toBeTruthy();
});

Note: There will be scenarios where you will use both of these setup processes together to get the best results.

注意:在某些情况下,您将同时使用这两个设置过程来获得最佳结果。

分组测试 (Grouping tests)

You can also group related tests together so you can isolate the setup and teardown functions. Grouping tests will also help you get a better overview of your different test cases.

您还可以将相关测试分组在一起,以便可以隔离设置和拆卸功能。 将测试分组还可以帮助您更好地了解不同的测试用例。

describe('testing factorial function', () => {beforeAll(() => {//do something})afterAll(() => {//do something})test("factorial of 3", () => {expect(factorialize(3)).toBe(6);});test("factorial of 5", () => {expect(factorialize(5)).toBe(120);});test("factorial of 3 is not 5", () => {expect(factorialize(3)).not.toBe(5);});
})

测试异步功能(Testing async functions)

It is common for Javascript code to run asynchronously using either promises or callbacks. The problem with testing asynchronous code is knowing when the code that you are testing is actually complete. Jest has several ways to handle this.

Java代码通常使用promise或callbacks异步运行。 测试异步代码的问题是知道您要测试的代码何时真正完成。 笑话有几种处理方法。

承诺: (Promises:)

Testing promises is straight forward in Jest. Just return the promise and Jest will wait for the promise to resolve. If the promise fails, the test will automatically fail as well.

在Jest中测试诺言很简单。 只需返回承诺,Jest将等待承诺解决。 如果承诺失败,则测试也会自动失败。

// string.js
const reverseString = str => {return new Promise((resolve, reject) => {if (!str) {reject("Empty string");return;}resolve(str.split("").reverse().join(""));});
};
module.exports = reverseString;// string.spec.js
const reverseString = require("./string");test(`reverseString 'String' to equal 'gnirtS'`, () => {return reverseString("String").then(str => {expect(str).toBe("gnirtS");});
});

You can also catch rejected promises using the catch() function.

您还可以使用catch()函数捕获被拒绝的承诺。

test(`reverseString '' to reject promise`, () => {return reverseString("String").catch(error => {expect(e).toMatch("Empty string");});
});

异步等待:(Async await:)

Alternatively, we can use async and await for testing promises.

另外,我们可以使用异步并等待测试诺言。

const reverseString = require("./string");test(`reverseString 'String' to equal 'gnirtS' using await`, async () => {const str = await reverseString("String")expect(str).toBe("gnirtS");
});

Note: You need to make your testing function async to use async and await.

注意:您需要使测试功能异步以使用异步和等待。

回调: (Callbacks:)

By default Jest tests complete once they reach the end of their execution which means that the test will be completed before the callback is called. This can be fixed by passing a single argument named done to your test function. Jest will wait until the done callback is called before finishing the test.

默认情况下,Jest测试在执行结束时会完成,这意味着测试将在调用回调之前完成。 可以通过将一个名为done的参数传递给测试函数来解决此问题。 Jest将等待直到完成的回调被调用,然后再完成测试。

// string.js
function reverseStringCallback(str, callback) {callback(str.split("").reverse().join(""))
}module.exports = {reverseStringCallback};// string.spec.js
const {reverseStringCallback} = require("./string");test(`reverseStringCallback 'string' to equal 'gnirts'`, (done) => {reverseStringCallback('string', (str) => {expect(str).toBe('gnirts')done()})
})

If done() is never called, the test will fail with a timeout error.

如果从不调用done() ,则测试将失败,并显示超时错误。

模拟 (Mocking)

Mocking is creating objects or services that simulate the behavior of real objects and plays a vital part in testing. The goal for mocking an object or function is to replace something we don’t control like an external service with something we do, that is why it’s essential that what we replace it with something that has all the features we need.

模拟正在创建对象或服务,以模拟真实对象的行为,并在测试中发挥至关重要的作用。 模拟对象或函数的目的是用我们所做的事情来替换我们无法控制的东西,例如外部服务,这就是为什么我们必须用具有我们所需的所有功能的东西来替换它的原因。

Using mocks also helps you to inspect information about your code e.g. if a function has already be called and which parameters were used.

使用模拟还可以帮助您检查有关代码的信息,例如是否已调用函数以及使用了哪些参数。

将Mocks传递给函数: (Passing Mocks to functions:)

One of the common ways to use the Mock function is by passing it as an argument to the function you are testing. This allows you to run your tests without importing the real dependencies and objects you would pass in your real application.

使用Mock函数的常见方法之一是将其作为参数传递给要测试的函数。 这使您可以运行测试,而无需导入将在实际应用程序中传递的实际依赖关系和对象。

const multiplyNumbers = (a, b, callback) => {callback(a * b);
};test("calls callback with arguments added", () => {const mockCallback = jest.fn();multiplyNumbers(1, 2, mockCallback);expect(mockCallback).toHaveBeenCalledWith(2);
});

This strategy is great but requires that your code supports dependency injection. If that is not the case you will need to mock already existing modules or functions instead.

这个策略很棒,但是需要您的代码支持依赖注入。 如果不是这种情况,则需要模拟已经存在的模块或函数。

模拟单个功能: (Mocking a single function:)

You can mock a single function using Jest.fn():

您可以使用Jest.fn()模拟单个函数:

const lodash = require('lodash')lodash.chunk = jest.fn(() => 'test')
test(`Test lodash chunk function`, () => {const result = lodash.chunk(['a', 'b', 'c', 'd'], 2)expect(result).toBe('test')expect(lodash.chunk).toHaveBeenCalled()expect(lodash.chunk).toHaveBeenCalledWith(['a', 'b', 'c', 'd'], 2)
})

Here I create a mock of the lodash.chunk function and test if it gets called and if the parameters are right.

在这里,我创建了lodash.chunk函数的模拟并测试它是否被调用以及参数是否正确。

模拟模块: (Mocking Modules:)

Mocking single functions works well if you only use one or two functions of a package or library but can get very cluttered when you need more functions of a module. Here we use jest.mock to automatically set the exports of an entire module instead of mocking the modules manually.

如果仅使用一个程序包或库的一个或两个功能,则模拟单个功能会很好地工作,但是当您需要一个模块的更多功能时,模拟会很混乱。 在这里,我们使用jest.mock来自动设置整个模块的导出,而不是手动模拟模块。

jest.mock('lodash');test(`Test lodash chunk function`, () => {const result = lodash.chunk(['a', 'b', 'c', 'd'], 2)expect(lodash.chunk).toHaveBeenCalled()expect(lodash.chunk).toHaveBeenCalledWith(['a', 'b', 'c', 'd'], 2)const concatResult = lodash.concat(2, [3], [[4]]);expect(lodash.concat).toHaveBeenCalled()expect(lodash.concat).toHaveBeenCalledWith(2, [3], [[4]])
})

As you can see I can now call all function of the lodash library as mock objects.

如您所见,我现在可以将lodash库的所有函数称为模拟对象。

The only disadvantage of this strategy is that it is difficult to access the original implementation of the module. For those use cases, you can use the spyOn function instead.

该策略的唯一缺点是很难访问该模块的原始实现。 对于这些用例,可以改用spyOn函数。

间谍包: (Spying packages:)

You can also spy on a package without creating a mock for it. This is done using the spyOn() function provided by Jest.

您也可以监视软件包而无需为其创建模拟。 这是通过Jest提供的spyOn()函数完成的。

const lodash = require('lodash')test(`Test lodash chunk function`, () => {const spy = jest.spyOn(lodash, 'chunk')const result = lodash.chunk(['a', 'b', 'c', 'd'], 2)expect(lodash.chunk).toHaveBeenCalled()expect(lodash.chunk).toHaveBeenCalledWith(['a', 'b', 'c', 'd'], 2)
})

您应该注意的重要事项(Important things you should look at)

There are a few more concepts you should definitely look at on your way to mastering testing in JavaScript. But I would greatly recommend learning the basics first and putting them into practice in your applications.

在掌握JavaScript测试方法的过程中,您肯定还应该考虑一些其他概念。 但是,我强烈建议您先学习基础知识,然后在您的应用程序中将其付诸实践。

  • Snapshot testing — Is used for testing the UI of your application

    快照测试-用于测试应用程序的UI

  • CI (Continous Integration) — The practice of automating the integration of code changes from multiple contributors into a single software project

    CI(持续集成)—将代码更改从多个贡献者自动集成到单个软件项目中的实践
  • CD (Continous Deployment) — Is a software release process that uses automated testing to validate if changes to a codebase are correct

    CD(持续部署)—是一种软件发布过程,使用自动测试来验证对代码库的更改是否正确
  • Automated dependency updates

    自动化的依赖项更新

It is vital to practice testing in your own applications. If you do it long enough, then you will master the art of testing and make your applications more stable and secure in the process.

在您自己的应用程序中进行测试至关重要。 如果您做的时间足够长,那么您将掌握测试的技巧,并使您的应用程序在过程中更加稳定和安全。

资料来源: (Sources:)

Here is a list of the sources I used for this article:

这是我用于本文的来源列表:

  • Official Jest documentation

    官方Jest文档

  • Introduction to Jest

    笑话简介

  • Understanding and mastering testing in Android

    了解和掌握Android中的测试

  • Testing best practices

    测试最佳做法

  • Understanding Jest Mocks

    了解笑话

结论(Conclusion)

You made it all the way until the end! I hope that this article helped you understand the basics of automated testing in JavaScript.

一路走到最后! 我希望本文能帮助您了解JavaScript自动测试的基础

If you have found this useful, please consider recommending and sharing it with other fellow developers. If you have any questions or feedback, let me know using my contact form or contact me on Twitter.

如果您发现此功能有用,请考虑推荐并与其他开发人员共享。 如果您有任何疑问或反馈,请使用我的联系表或通过Twitter与我联系。

Originally published at https://gabrieltanner.org.

最初在https://gabrieltanner.org上发布

翻译自: https://medium.com/dailyjs/an-introduction-to-testing-in-javascript-26ed0a6da6f4

javascript测试

查看全文
如若内容造成侵权/违法违规/事实不符,请联系编程学习网邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

相关文章

  1. L2-constrained Softmax Loss for Discriminative Face Verification

    https://arxiv.org/abs/1703.09507 摘要 近年来&#xff0c;利用深度卷积神经网络(DCNNs)&#xff0c;人脸验证系统的性能得到了显著改善。一个典型的人脸验证流程包括:使用softmax loss训练一个用于目标分类的深度网络&#xff0c;使用倒数第二层输出作为特征描述子&#xff…...

    2024/4/20 16:00:57
  2. 北京双眼皮出名医生 名扬王世勇a

    ...

    2024/4/21 13:17:57
  3. 论一个前端开发者的自我修养

    先做个简单的自我介绍&#xff1a;本人&#xff08;大名&#xff1a;萧文翰&#xff09;&#xff0c;Android 架构师/技术顾问。从2013年开始从事移动前端开发&#xff0c;主攻 Android 和跨平台开发技术&#xff0c;具有丰富的实战项目经验。国内7项专利共同发明人&#xff1b…...

    2024/4/21 13:17:56
  4. 割双眼皮几年后会变窄吗

    ...

    2024/4/20 15:09:14
  5. 双眼皮整形哪家好可找金宪俊

    ...

    2024/4/21 13:17:54
  6. angular使用 base64位格式图片/ionic使用 base64位图片/vue使用base64位图片

    在写微信支付二维码的时候&#xff0c;返回的是 base64 格式的图片。 网上查了一大堆都是base64转jpg&#xff0c;都是通过file保存到本地再读取&#xff0c;其实可以通过img标签直接加载 例 使用原生的标签<img> 代替 <image> 标签即可例如&#xff1a;<img s…...

    2024/4/21 13:17:56
  7. img不刷新问题

    今天在做ajax上传图片时&#xff1a;由于新上传的图片会处理成和原来的图片名称一样&#xff0c;这样就造成img标签的src看起来是没有变化的&#xff0c;所有就不会重新请求图片。 解决办法&#xff1a;srcurl?new date().getTime(),给加上时间戳...

    2024/4/21 13:17:51
  8. vue中动态设置img的src属性

    我们知道在使用img标签时&#xff0c;直接在img标签中通过src属性设置图片的路径&#xff0c;即可表示对图片的引用。 <img src"../assets/drag_1.jpg"> 如上表示的是 引用的是assets文件下的1.jpg图片。这个对于所有的前端开发者来说&#xff0c;都是没有什…...

    2024/4/21 13:17:50
  9. ubuntu上利用virtualbox安装windowsXP

    参照网页:http://forum.ubuntu.org.cn/viewtopic.php?f=65&t=60618默认的host键是指右边的Ctrl键1. 下载virtualbox(选择VirtualBox 4.0.12 for Linux hosts)http://www.virtualbox.org/wiki/Downloads2. 安装virtualboxsudo dpkg -i 下载的文件名.deb3. 如果没有windo…...

    2024/4/21 13:17:50
  10. 双眼皮埋线哪里好只择金宪俊

    ...

    2024/4/21 13:17:48
  11. 双眼皮埋线哪好认定金宪俊

    ...

    2024/4/21 13:17:47
  12. AngularJS学习记录-过滤器(排序方式过滤)

    AngularJS学习记录-过滤器(排序方式过滤) (1)功能描述 在页面的视图模板中&#xff0c;调用排序过滤器&#xff0c;将显示的数据按”score”属性值降序进行排列&#xff0c;并且值显示前3条数据记录。 (2)实现代码 <!DOCTYPE html> <html lang"en"> …...

    2024/5/3 16:32:16
  13. 双眼皮埋线哪儿好直问金宪俊

    ...

    2024/4/20 15:15:00
  14. 埋线双眼皮哪里好就到金宪俊

    ...

    2024/4/20 4:09:12
  15. 【正则表达式】-input只能输入数字

    问题&#xff1a; 在做angular前端项目时要求时间输入框只能输入数字&#xff08;小数点也不能输入&#xff09;。 实现&#xff1a; 在.html中用正则表达式来进行input的输入限制。 代码&#xff1a; <input onkeyup"this.valuethis.value.replace(/\D/g,)"ona…...

    2024/4/20 16:01:02
  16. 【后端】【正则表达式】-【校验15位或18位身份证号码】

    15位身份证号码&#xff1a; ([1-9][0-9]{5}[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}) 18位身份证号码&#xff1a; ([1-9][0-9]{5}(18|19|20)[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}[0-9Xx]) 同时校验&#xff1a; (…...

    2024/5/2 20:51:50
  17. Angular 统一社会信用代码生成校验

    地址 项目代码 https://files-cdn.cnblogs.com/files/linyisonger/webtoolbox.zip 展示效果 介绍 使用angular前端框架&#xff0c;采用 github actions技术&#xff0c;将项目打包成docker镜像&#xff0c;再连接自己的服务器更新镜像与容器&#xff0c;达到持续集成的效果。…...

    2024/4/21 13:17:45
  18. 最严谨的校验email地址的正则表达式

    通用 (?:[a-z0-9!#$%&*/?^_{|}~-](?:\.[a-z0-9!#$%&*/?^_{|}~-])*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?…...

    2024/4/21 13:17:45
  19. js 正则验证多个ip地址并用逗号分隔

    //检测多个ip地址用逗号分隔function checkIpv6(ip) {var reg /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/var valdata ip.split(,);for(i0;i<valdata.length;i){if(r…...

    2024/5/1 16:49:31
  20. AngularJS进阶 十三 JS利用正则表达式校验手机号

    JS利用正则表达式校验手机号注:请点击此处进行充电&#xff01;绪由于项目需求&#xff0c;需要在前端实现手机号码的校验。当然了&#xff0c;对于基本的格式校验应该放在客户端进行&#xff0c;而不需要再将待校验的手机号发送至服务端&#xff0c;在服务端完成校验&#xff…...

    2024/4/21 13:17:42

最新文章

  1. 【Linux学习】(2)OS的简单了解|Linux的基本指令操作

    前言 本文将先简单了解什么是操作系统&#xff0c;再讲解一些Linux的基本指令。 一、操作系统的简单了解 1、什么是操作系统&#xff08;Operating System&#xff0c;简称OS&#xff09;&#xff1f; OS是一款做软硬件管理的软件。软硬件的体系结构图&#xff1a; 硬件&…...

    2024/5/3 17:21:17
  2. 梯度消失和梯度爆炸的一些处理方法

    在这里是记录一下梯度消失或梯度爆炸的一些处理技巧。全当学习总结了如有错误还请留言&#xff0c;在此感激不尽。 权重和梯度的更新公式如下&#xff1a; w w − η ⋅ ∇ w w w - \eta \cdot \nabla w ww−η⋅∇w 个人通俗的理解梯度消失就是网络模型在反向求导的时候出…...

    2024/3/20 10:50:27
  3. Linux命令-dpkg-reconfigure命令(Debian Linux中重新配制一个已经安装的软件包)

    说明 dpkg-reconfigure命令 是Debian Linux中重新配置已经安装过的软件包&#xff0c;可以将一个或者多个已 安装的软件包传递给此指令&#xff0c;它将询问软件初次安装后的配置问题。 当用户需要再次对软件包配置的时候&#xff0c;可以使用dpkg-reconfigure命令来对指定的软…...

    2024/4/30 4:15:04
  4. 微信小程序实现左滑删除

    效果 实现思路 使用的是官方提供的movable-area 嵌套movable-view 1、movable-area&#xff1a;注意点&#xff0c;需要设置其高度&#xff0c;否则会出现列表内容重叠的现象。 2、由于movable-view需要向右移动&#xff0c;左滑的时候给删除控件展示的空间&#xff0c;故 mov…...

    2024/5/1 13:30:10
  5. 技术与安全的交织

    引言 介绍数字化转型对企业出海策略的影响&#xff0c;强调在全球市场中成功的关键因素之一是有效利用网络技术&#xff0c;如SOCKS5代理、代理IP&#xff0c;以及确保网络安全。 第一部分&#xff1a;网络技术的基础 SOCKS5代理 定义和工作原理 SOCKS5代理与网络匿名性的关系…...

    2024/5/1 16:32:06
  6. 【外汇早评】美通胀数据走低,美元调整

    原标题:【外汇早评】美通胀数据走低,美元调整昨日美国方面公布了新一期的核心PCE物价指数数据,同比增长1.6%,低于前值和预期值的1.7%,距离美联储的通胀目标2%继续走低,通胀压力较低,且此前美国一季度GDP初值中的消费部分下滑明显,因此市场对美联储后续更可能降息的政策…...

    2024/5/1 17:30:59
  7. 【原油贵金属周评】原油多头拥挤,价格调整

    原标题:【原油贵金属周评】原油多头拥挤,价格调整本周国际劳动节,我们喜迎四天假期,但是整个金融市场确实流动性充沛,大事频发,各个商品波动剧烈。美国方面,在本周四凌晨公布5月份的利率决议和新闻发布会,维持联邦基金利率在2.25%-2.50%不变,符合市场预期。同时美联储…...

    2024/5/2 16:16:39
  8. 【外汇周评】靓丽非农不及疲软通胀影响

    原标题:【外汇周评】靓丽非农不及疲软通胀影响在刚结束的周五,美国方面公布了新一期的非农就业数据,大幅好于前值和预期,新增就业重新回到20万以上。具体数据: 美国4月非农就业人口变动 26.3万人,预期 19万人,前值 19.6万人。 美国4月失业率 3.6%,预期 3.8%,前值 3…...

    2024/4/29 2:29:43
  9. 【原油贵金属早评】库存继续增加,油价收跌

    原标题:【原油贵金属早评】库存继续增加,油价收跌周三清晨公布美国当周API原油库存数据,上周原油库存增加281万桶至4.692亿桶,增幅超过预期的74.4万桶。且有消息人士称,沙特阿美据悉将于6月向亚洲炼油厂额外出售更多原油,印度炼油商预计将每日获得至多20万桶的额外原油供…...

    2024/5/2 9:28:15
  10. 【外汇早评】日本央行会议纪要不改日元强势

    原标题:【外汇早评】日本央行会议纪要不改日元强势近两日日元大幅走强与近期市场风险情绪上升,避险资金回流日元有关,也与前一段时间的美日贸易谈判给日本缓冲期,日本方面对汇率问题也避免继续贬值有关。虽然今日早间日本央行公布的利率会议纪要仍然是支持宽松政策,但这符…...

    2024/4/27 17:58:04
  11. 【原油贵金属早评】欧佩克稳定市场,填补伊朗问题的影响

    原标题:【原油贵金属早评】欧佩克稳定市场,填补伊朗问题的影响近日伊朗局势升温,导致市场担忧影响原油供给,油价试图反弹。此时OPEC表态稳定市场。据消息人士透露,沙特6月石油出口料将低于700万桶/日,沙特已经收到石油消费国提出的6月份扩大出口的“适度要求”,沙特将满…...

    2024/4/27 14:22:49
  12. 【外汇早评】美欲与伊朗重谈协议

    原标题:【外汇早评】美欲与伊朗重谈协议美国对伊朗的制裁遭到伊朗的抗议,昨日伊朗方面提出将部分退出伊核协议。而此行为又遭到欧洲方面对伊朗的谴责和警告,伊朗外长昨日回应称,欧洲国家履行它们的义务,伊核协议就能保证存续。据传闻伊朗的导弹已经对准了以色列和美国的航…...

    2024/4/28 1:28:33
  13. 【原油贵金属早评】波动率飙升,市场情绪动荡

    原标题:【原油贵金属早评】波动率飙升,市场情绪动荡因中美贸易谈判不安情绪影响,金融市场各资产品种出现明显的波动。随着美国与中方开启第十一轮谈判之际,美国按照既定计划向中国2000亿商品征收25%的关税,市场情绪有所平复,已经开始接受这一事实。虽然波动率-恐慌指数VI…...

    2024/4/30 9:43:09
  14. 【原油贵金属周评】伊朗局势升温,黄金多头跃跃欲试

    原标题:【原油贵金属周评】伊朗局势升温,黄金多头跃跃欲试美国和伊朗的局势继续升温,市场风险情绪上升,避险黄金有向上突破阻力的迹象。原油方面稍显平稳,近期美国和OPEC加大供给及市场需求回落的影响,伊朗局势并未推升油价走强。近期中美贸易谈判摩擦再度升级,美国对中…...

    2024/4/27 17:59:30
  15. 【原油贵金属早评】市场情绪继续恶化,黄金上破

    原标题:【原油贵金属早评】市场情绪继续恶化,黄金上破周初中国针对于美国加征关税的进行的反制措施引发市场情绪的大幅波动,人民币汇率出现大幅的贬值动能,金融市场受到非常明显的冲击。尤其是波动率起来之后,对于股市的表现尤其不安。隔夜美国股市出现明显的下行走势,这…...

    2024/5/2 15:04:34
  16. 【外汇早评】美伊僵持,风险情绪继续升温

    原标题:【外汇早评】美伊僵持,风险情绪继续升温昨日沙特两艘油轮再次发生爆炸事件,导致波斯湾局势进一步恶化,市场担忧美伊可能会出现摩擦生火,避险品种获得支撑,黄金和日元大幅走强。美指受中美贸易问题影响而在低位震荡。继5月12日,四艘商船在阿联酋领海附近的阿曼湾、…...

    2024/4/28 1:34:08
  17. 【原油贵金属早评】贸易冲突导致需求低迷,油价弱势

    原标题:【原油贵金属早评】贸易冲突导致需求低迷,油价弱势近日虽然伊朗局势升温,中东地区几起油船被袭击事件影响,但油价并未走高,而是出于调整结构中。由于市场预期局势失控的可能性较低,而中美贸易问题导致的全球经济衰退风险更大,需求会持续低迷,因此油价调整压力较…...

    2024/4/26 19:03:37
  18. 氧生福地 玩美北湖(上)——为时光守候两千年

    原标题:氧生福地 玩美北湖(上)——为时光守候两千年一次说走就走的旅行,只有一张高铁票的距离~ 所以,湖南郴州,我来了~ 从广州南站出发,一个半小时就到达郴州西站了。在动车上,同时改票的南风兄和我居然被分到了一个车厢,所以一路非常愉快地聊了过来。 挺好,最起…...

    2024/4/29 20:46:55
  19. 氧生福地 玩美北湖(中)——永春梯田里的美与鲜

    原标题:氧生福地 玩美北湖(中)——永春梯田里的美与鲜一觉醒来,因为大家太爱“美”照,在柳毅山庄去寻找龙女而错过了早餐时间。近十点,向导坏坏还是带着饥肠辘辘的我们去吃郴州最富有盛名的“鱼头粉”。说这是“十二分推荐”,到郴州必吃的美食之一。 哇塞!那个味美香甜…...

    2024/4/30 22:21:04
  20. 氧生福地 玩美北湖(下)——奔跑吧骚年!

    原标题:氧生福地 玩美北湖(下)——奔跑吧骚年!让我们红尘做伴 活得潇潇洒洒 策马奔腾共享人世繁华 对酒当歌唱出心中喜悦 轰轰烈烈把握青春年华 让我们红尘做伴 活得潇潇洒洒 策马奔腾共享人世繁华 对酒当歌唱出心中喜悦 轰轰烈烈把握青春年华 啊……啊……啊 两…...

    2024/5/1 4:32:01
  21. 扒开伪装医用面膜,翻六倍价格宰客,小姐姐注意了!

    原标题:扒开伪装医用面膜,翻六倍价格宰客,小姐姐注意了!扒开伪装医用面膜,翻六倍价格宰客!当行业里的某一品项火爆了,就会有很多商家蹭热度,装逼忽悠,最近火爆朋友圈的医用面膜,被沾上了污点,到底怎么回事呢? “比普通面膜安全、效果好!痘痘、痘印、敏感肌都能用…...

    2024/4/27 23:24:42
  22. 「发现」铁皮石斛仙草之神奇功效用于医用面膜

    原标题:「发现」铁皮石斛仙草之神奇功效用于医用面膜丽彦妆铁皮石斛医用面膜|石斛多糖无菌修护补水贴19大优势: 1、铁皮石斛:自唐宋以来,一直被列为皇室贡品,铁皮石斛生于海拔1600米的悬崖峭壁之上,繁殖力差,产量极低,所以古代仅供皇室、贵族享用 2、铁皮石斛自古民间…...

    2024/4/28 5:48:52
  23. 丽彦妆\医用面膜\冷敷贴轻奢医学护肤引导者

    原标题:丽彦妆\医用面膜\冷敷贴轻奢医学护肤引导者【公司简介】 广州华彬企业隶属香港华彬集团有限公司,专注美业21年,其旗下品牌: 「圣茵美」私密荷尔蒙抗衰,产后修复 「圣仪轩」私密荷尔蒙抗衰,产后修复 「花茵莳」私密荷尔蒙抗衰,产后修复 「丽彦妆」专注医学护…...

    2024/4/30 9:42:22
  24. 广州械字号面膜生产厂家OEM/ODM4项须知!

    原标题:广州械字号面膜生产厂家OEM/ODM4项须知!广州械字号面膜生产厂家OEM/ODM流程及注意事项解读: 械字号医用面膜,其实在我国并没有严格的定义,通常我们说的医美面膜指的应该是一种「医用敷料」,也就是说,医用面膜其实算作「医疗器械」的一种,又称「医用冷敷贴」。 …...

    2024/5/2 9:07:46
  25. 械字号医用眼膜缓解用眼过度到底有无作用?

    原标题:械字号医用眼膜缓解用眼过度到底有无作用?医用眼膜/械字号眼膜/医用冷敷眼贴 凝胶层为亲水高分子材料,含70%以上的水分。体表皮肤温度传导到本产品的凝胶层,热量被凝胶内水分子吸收,通过水分的蒸发带走大量的热量,可迅速地降低体表皮肤局部温度,减轻局部皮肤的灼…...

    2024/4/30 9:42:49
  26. 配置失败还原请勿关闭计算机,电脑开机屏幕上面显示,配置失败还原更改 请勿关闭计算机 开不了机 这个问题怎么办...

    解析如下&#xff1a;1、长按电脑电源键直至关机&#xff0c;然后再按一次电源健重启电脑&#xff0c;按F8健进入安全模式2、安全模式下进入Windows系统桌面后&#xff0c;按住“winR”打开运行窗口&#xff0c;输入“services.msc”打开服务设置3、在服务界面&#xff0c;选中…...

    2022/11/19 21:17:18
  27. 错误使用 reshape要执行 RESHAPE,请勿更改元素数目。

    %读入6幅图像&#xff08;每一幅图像的大小是564*564&#xff09; f1 imread(WashingtonDC_Band1_564.tif); subplot(3,2,1),imshow(f1); f2 imread(WashingtonDC_Band2_564.tif); subplot(3,2,2),imshow(f2); f3 imread(WashingtonDC_Band3_564.tif); subplot(3,2,3),imsho…...

    2022/11/19 21:17:16
  28. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机...

    win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”问题的解决方法在win7系统关机时如果有升级系统的或者其他需要会直接进入一个 等待界面&#xff0c;在等待界面中我们需要等待操作结束才能关机&#xff0c;虽然这比较麻烦&#xff0c;但是对系统进行配置和升级…...

    2022/11/19 21:17:15
  29. 台式电脑显示配置100%请勿关闭计算机,“准备配置windows 请勿关闭计算机”的解决方法...

    有不少用户在重装Win7系统或更新系统后会遇到“准备配置windows&#xff0c;请勿关闭计算机”的提示&#xff0c;要过很久才能进入系统&#xff0c;有的用户甚至几个小时也无法进入&#xff0c;下面就教大家这个问题的解决方法。第一种方法&#xff1a;我们首先在左下角的“开始…...

    2022/11/19 21:17:14
  30. win7 正在配置 请勿关闭计算机,怎么办Win7开机显示正在配置Windows Update请勿关机...

    置信有很多用户都跟小编一样遇到过这样的问题&#xff0c;电脑时发现开机屏幕显现“正在配置Windows Update&#xff0c;请勿关机”(如下图所示)&#xff0c;而且还需求等大约5分钟才干进入系统。这是怎样回事呢&#xff1f;一切都是正常操作的&#xff0c;为什么开时机呈现“正…...

    2022/11/19 21:17:13
  31. 准备配置windows 请勿关闭计算机 蓝屏,Win7开机总是出现提示“配置Windows请勿关机”...

    Win7系统开机启动时总是出现“配置Windows请勿关机”的提示&#xff0c;没过几秒后电脑自动重启&#xff0c;每次开机都这样无法进入系统&#xff0c;此时碰到这种现象的用户就可以使用以下5种方法解决问题。方法一&#xff1a;开机按下F8&#xff0c;在出现的Windows高级启动选…...

    2022/11/19 21:17:12
  32. 准备windows请勿关闭计算机要多久,windows10系统提示正在准备windows请勿关闭计算机怎么办...

    有不少windows10系统用户反映说碰到这样一个情况&#xff0c;就是电脑提示正在准备windows请勿关闭计算机&#xff0c;碰到这样的问题该怎么解决呢&#xff0c;现在小编就给大家分享一下windows10系统提示正在准备windows请勿关闭计算机的具体第一种方法&#xff1a;1、2、依次…...

    2022/11/19 21:17:11
  33. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”的解决方法...

    今天和大家分享一下win7系统重装了Win7旗舰版系统后&#xff0c;每次关机的时候桌面上都会显示一个“配置Windows Update的界面&#xff0c;提示请勿关闭计算机”&#xff0c;每次停留好几分钟才能正常关机&#xff0c;导致什么情况引起的呢&#xff1f;出现配置Windows Update…...

    2022/11/19 21:17:10
  34. 电脑桌面一直是清理请关闭计算机,windows7一直卡在清理 请勿关闭计算机-win7清理请勿关机,win7配置更新35%不动...

    只能是等着&#xff0c;别无他法。说是卡着如果你看硬盘灯应该在读写。如果从 Win 10 无法正常回滚&#xff0c;只能是考虑备份数据后重装系统了。解决来方案一&#xff1a;管理员运行cmd&#xff1a;net stop WuAuServcd %windir%ren SoftwareDistribution SDoldnet start WuA…...

    2022/11/19 21:17:09
  35. 计算机配置更新不起,电脑提示“配置Windows Update请勿关闭计算机”怎么办?

    原标题&#xff1a;电脑提示“配置Windows Update请勿关闭计算机”怎么办&#xff1f;win7系统中在开机与关闭的时候总是显示“配置windows update请勿关闭计算机”相信有不少朋友都曾遇到过一次两次还能忍但经常遇到就叫人感到心烦了遇到这种问题怎么办呢&#xff1f;一般的方…...

    2022/11/19 21:17:08
  36. 计算机正在配置无法关机,关机提示 windows7 正在配置windows 请勿关闭计算机 ,然后等了一晚上也没有关掉。现在电脑无法正常关机...

    关机提示 windows7 正在配置windows 请勿关闭计算机 &#xff0c;然后等了一晚上也没有关掉。现在电脑无法正常关机以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;关机提示 windows7 正在配…...

    2022/11/19 21:17:05
  37. 钉钉提示请勿通过开发者调试模式_钉钉请勿通过开发者调试模式是真的吗好不好用...

    钉钉请勿通过开发者调试模式是真的吗好不好用 更新时间:2020-04-20 22:24:19 浏览次数:729次 区域: 南阳 > 卧龙 列举网提醒您:为保障您的权益,请不要提前支付任何费用! 虚拟位置外设器!!轨迹模拟&虚拟位置外设神器 专业用于:钉钉,外勤365,红圈通,企业微信和…...

    2022/11/19 21:17:05
  38. 配置失败还原请勿关闭计算机怎么办,win7系统出现“配置windows update失败 还原更改 请勿关闭计算机”,长时间没反应,无法进入系统的解决方案...

    前几天班里有位学生电脑(windows 7系统)出问题了&#xff0c;具体表现是开机时一直停留在“配置windows update失败 还原更改 请勿关闭计算机”这个界面&#xff0c;长时间没反应&#xff0c;无法进入系统。这个问题原来帮其他同学也解决过&#xff0c;网上搜了不少资料&#x…...

    2022/11/19 21:17:04
  39. 一个电脑无法关闭计算机你应该怎么办,电脑显示“清理请勿关闭计算机”怎么办?...

    本文为你提供了3个有效解决电脑显示“清理请勿关闭计算机”问题的方法&#xff0c;并在最后教给你1种保护系统安全的好方法&#xff0c;一起来看看&#xff01;电脑出现“清理请勿关闭计算机”在Windows 7(SP1)和Windows Server 2008 R2 SP1中&#xff0c;添加了1个新功能在“磁…...

    2022/11/19 21:17:03
  40. 请勿关闭计算机还原更改要多久,电脑显示:配置windows更新失败,正在还原更改,请勿关闭计算机怎么办...

    许多用户在长期不使用电脑的时候&#xff0c;开启电脑发现电脑显示&#xff1a;配置windows更新失败&#xff0c;正在还原更改&#xff0c;请勿关闭计算机。。.这要怎么办呢&#xff1f;下面小编就带着大家一起看看吧&#xff01;如果能够正常进入系统&#xff0c;建议您暂时移…...

    2022/11/19 21:17:02
  41. 还原更改请勿关闭计算机 要多久,配置windows update失败 还原更改 请勿关闭计算机,电脑开机后一直显示以...

    配置windows update失败 还原更改 请勿关闭计算机&#xff0c;电脑开机后一直显示以以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;配置windows update失败 还原更改 请勿关闭计算机&#x…...

    2022/11/19 21:17:01
  42. 电脑配置中请勿关闭计算机怎么办,准备配置windows请勿关闭计算机一直显示怎么办【图解】...

    不知道大家有没有遇到过这样的一个问题&#xff0c;就是我们的win7系统在关机的时候&#xff0c;总是喜欢显示“准备配置windows&#xff0c;请勿关机”这样的一个页面&#xff0c;没有什么大碍&#xff0c;但是如果一直等着的话就要两个小时甚至更久都关不了机&#xff0c;非常…...

    2022/11/19 21:17:00
  43. 正在准备配置请勿关闭计算机,正在准备配置windows请勿关闭计算机时间长了解决教程...

    当电脑出现正在准备配置windows请勿关闭计算机时&#xff0c;一般是您正对windows进行升级&#xff0c;但是这个要是长时间没有反应&#xff0c;我们不能再傻等下去了。可能是电脑出了别的问题了&#xff0c;来看看教程的说法。正在准备配置windows请勿关闭计算机时间长了方法一…...

    2022/11/19 21:16:59
  44. 配置失败还原请勿关闭计算机,配置Windows Update失败,还原更改请勿关闭计算机...

    我们使用电脑的过程中有时会遇到这种情况&#xff0c;当我们打开电脑之后&#xff0c;发现一直停留在一个界面&#xff1a;“配置Windows Update失败&#xff0c;还原更改请勿关闭计算机”&#xff0c;等了许久还是无法进入系统。如果我们遇到此类问题应该如何解决呢&#xff0…...

    2022/11/19 21:16:58
  45. 如何在iPhone上关闭“请勿打扰”

    Apple’s “Do Not Disturb While Driving” is a potentially lifesaving iPhone feature, but it doesn’t always turn on automatically at the appropriate time. For example, you might be a passenger in a moving car, but your iPhone may think you’re the one dri…...

    2022/11/19 21:16:57