Skip to content

Commit 3ddb132

Browse files
committed
docs: update mocking-classes
1 parent fa49ad1 commit 3ddb132

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

guide/mocking/classes.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Dog {
2727
}
2828
```
2929

30-
We can re-create this class with `vi.fn` (or `vi.spyOn().mockImplementation()`):
30+
我们可以使用 `vi.fn`(或 `vi.spyOn().mockImplementation()`)来重新创建这个类:
3131

3232
```ts
3333
const Dog = vi.fn(class {
@@ -44,7 +44,7 @@ const Dog = vi.fn(class {
4444
```
4545

4646
::: warning
47-
If a non-primitive is returned from the constructor function, that value will become the result of the new expression. In this case the `[[Prototype]]` may not be correctly bound:
47+
如果构造函数返回一个非原始值,那么该值将成为 new 表达式的结果。在这种情况下,`[[Prototype]]` 可能无法正确绑定:
4848

4949
```ts
5050
const CorrectDogClass = vi.fn(function (name) {
@@ -62,11 +62,11 @@ Marti instanceof CorrectDogClass // ✅ true
6262
Newt instanceof IncorrectDogClass // ❌ false!
6363
```
6464

65-
If you are mocking classes, prefer the class syntax over the function.
65+
如果你正在模拟类,建议优先使用类语法而不是函数语法。
6666
:::
6767

68-
::: tip WHEN TO USE?
69-
Generally speaking, you would re-create a class like this inside the module factory if the class is re-exported from another module:
68+
::: tip 何时使用?
69+
一般来说,如果类是从另一个模块重新导出的,你会在模块工厂内部重新创建这样的类:
7070

7171
```ts
7272
import { Dog } from './dog.js'
@@ -80,7 +80,7 @@ vi.mock(import('./dog.js'), () => {
8080
})
8181
```
8282

83-
This method can also be used to pass an instance of a class to a function that accepts the same interface:
83+
这种方法也可以用于将类的实例传递给接受相同接口的函数:
8484

8585
```ts [src/feed.ts]
8686
function feed(dog: Dog) {
@@ -106,39 +106,39 @@ test('can feed dogs', () => {
106106
```
107107
:::
108108

109-
Now, when we create a new instance of the `Dog` class its `speak` method (alongside `feed` and `greet`) is already mocked:
109+
现在,当我们创建 `Dog` 类的新实例时,它的 `speak` 方法(以及 `feed` `greet` 方法)已经被模拟了:
110110

111111
```ts
112112
const Cooper = new Dog('Cooper')
113113
Cooper.speak() // loud bark!
114114
Cooper.greet() // Hi! My name is Cooper!
115115

116-
// you can use built-in assertions to check the validity of the call
116+
// 你可以使用内置断言来检查调用的有效性
117117
expect(Cooper.speak).toHaveBeenCalled()
118118
expect(Cooper.greet).toHaveBeenCalled()
119119

120120
const Max = new Dog('Max')
121121

122-
// methods are not shared between instances if you assigned them directly
122+
// 如果你直接赋值方法,这些方法在实例之间不会共享
123123
expect(Max.speak).not.toHaveBeenCalled()
124124
expect(Max.greet).not.toHaveBeenCalled()
125125
```
126126

127-
We can reassign the return value for a specific instance:
127+
我们可以为特定实例重新分配返回值:
128128

129129
```ts
130130
const dog = new Dog('Cooper')
131131

132-
// "vi.mocked" is a type helper, since
133-
// TypeScript doesn't know that Dog is a mocked class,
134-
// it wraps any function in a Mock<T> type
135-
// without validating if the function is a mock
132+
// "vi.mocked" 是一个类型辅助工具,因为
133+
// TypeScript 不知道 Dog 是一个被模拟的类,
134+
// 它将任何函数包装在 Mock<T> 类型中
135+
// 而不会验证该函数是否为模拟函数
136136
vi.mocked(dog.speak).mockReturnValue('woof woof')
137137

138138
dog.speak() // woof woof
139139
```
140140

141-
To mock the property, we can use the `vi.spyOn(dog, 'name', 'get')` method. This makes it possible to use spy assertions on the mocked property:
141+
要模拟属性,我们可以使用 `vi.spyOn(dog, 'name', 'get')` 方法。这样就可以在模拟属性上使用间谍断言:
142142

143143
```ts
144144
const dog = new Dog('Cooper')
@@ -150,9 +150,9 @@ expect(nameSpy).toHaveBeenCalledTimes(1)
150150
```
151151

152152
::: tip
153-
You can also spy on getters and setters using the same method.
153+
你也可以使用相同的方法来监视 getter 和 setter。
154154
:::
155155

156156
::: danger
157-
Using classes with `vi.fn()` was introduced in Vitest 4. Previously, you had to use `function` and `prototype` inheritence directly. See [v3 guide](https://v3.vitest.dev/guide/mocking.html#classes).
157+
在 Vitest 4 中引入了使用 `vi.fn()` 模拟类的功能。在此之前,你必须直接使用 `function` `prototype` 继承。参见 [v3 指南](https://v3.vitest.dev/guide/mocking.html#classes)
158158
:::

0 commit comments

Comments
 (0)