gpt4 book ai didi

reactjs - spy 模块和模拟模块功能

转载 作者:行者123 更新时间:2023-12-03 17:11:52 25 4
gpt4 key购买 nike

我正在为我的组件编写一些测试,但我在这里遇到了一些麻烦......

这是我的游戏组件:

import React from 'react';
import User from './User';
import Board from './Board';
import Deck from './Deck';

class Game extends React.Component {
constructor(props) {
super(props);
this.board = new Board();
this.deck = new Deck();

//some more code
}

componentDidMount() {
this.initializeUser("xpto");
//some more code
}

//some more code

initializeUser(name) {
const user = new User(name, this.deck);
//some more code

user.pickCards();
//some more code
}

//some more code

render() {
return (
<div className="game-container">
something to show
<div id="deck"></div>
</div>
);
}
}


Game.propTypes = {
};

Game.defaultProps = {
};

export default Game;

我的董事会类(class):
export default class Board {
//some code
}

我的甲板课:
export default class Deck {
constructor(props) {
//some more code

this.cardsLeft = 52;
this.lastPick = 0;

//some more code
}

pickCards() {
this.lastPick = 4;
this.cardsLeft -= this.lastPick;
const deckElem = document.getElementById("deck");
deckElem.innerHTML = this.cardsLeft;
return this.lastPick;
}

//some more code
}

我的用户类:
class User {
constructor(name, deck) {
this.name = name;
this.tableDeck = deck;
this.cards = 0;
//some more code
}

//some more code

pickCards() {
const newCards = this.tableDeck.pickCards();
this.cards += newCards;
//some code
}

//some more code
}

export default User;

现在,在我的测试中,我试图测试 BoardUser被调用,如果 pickCards()也被称为。

这是我的测试:
import React from 'react';
import { mount } from 'enzyme';
import Game from './Game';
import User from './User';
import Board from './Board';

describe('Game start', () => {
let container;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
document.body.removeChild(container);
container = null;
});

it("test where I'm having problems", () => {
const boardSpy = jest.spyOn(Board, 'constructor'),
userSpy = jest.spyOn(User, 'constructor'),
pickCardMock = jest.fn();

User.pickCard = pickCardMock;

const wrapper = mount(<Game />, { attachTo: container });

expect(boardSpy).toHaveBeenCalledTimes(1);
expect(userSpy).toHaveBeenCalledTimes(1);
expect(pickCardMock).toHaveBeenCalledTimes(1);

//some more code
});

it("example test where I need to test everything without mocks", () => {
const wrapper = mount(<Game />, { attachTo: container });

expect(wrapper.find("#deck").text()).toEqual('48');

//some code
});

//some more tests
});

我不想 mock BoardUser因为我需要一切正常工作。但我想监视他们以检查他们是否真的被召唤。我想模拟 pickCard()来自 User .

我已经尝试使用 jest.mock('./board');require('board') (例如)仅在我的 it() 内测试,但它没有工作。现在我正试图监视组件构造函数。

但是 expect(boardSpy).toHaveBeenCalledTimes(1)未能说被称为 0 次而不是 1 次。

pickCardMock似乎没有链接到 User模块因为调试时 pickCard是普通函数而不是模拟函数和 expect(pickCardMock).toHaveBeenCalledTimes(1)也收到 0 而不是 1。

任何人都知道如何解决这两个问题(监视一个模块并在另一个模块中模拟一个函数)?

再次:

I don't want to mock things for the whole test suite, I just want to mock for a single test (and I want to be able to spy a module call).



您可以找到所有这些代码 here .

最佳答案

您可以使用jest.mock(moduleName, factory, options)模拟UserBoard组件。

例如。
Game.jsx :

import React, { Component } from 'react';
import User from './User';
import Board from './Board';

class Game extends Component {
constructor(props) {
super(props);
this.board = new Board({});
}

initializeUser(name) {
const user = new User(name);
user.pickCards();
}

render() {
return <div className="game-container"></div>;
}
}

export default Game;
Board.jsx :

import React, { Component } from 'react';

class Board extends Component {
constructor(props) {
super(props);
}
render() {
return 'Board';
}
}

export default Board;
User.jsx :

import React, { Component } from 'react';

class User extends Component {
constructor(props) {
super(props);
this.name = this.props.name;
}

pickCards() {
console.log('pick cards real implementation');
}

render() {
return 'User';
}
}

export default User;
Game.test.jsx :

import React from 'react';
import { mount } from 'enzyme';
import Game from './Game';
import BoardMock from './Board';
import UserMock from './User';

jest.mock('./User', () => {
const mUser = { pickCards: jest.fn() };
return jest.fn(() => mUser);
});

jest.mock('./Board', () => jest.fn());

describe('62199135', () => {
afterAll(() => {
jest.resetAllMocks();
});
it('should pass', () => {
const userMock = new UserMock();
const wrapper = mount(<Game />);
const gameInstance = wrapper.instance();
gameInstance.initializeUser('some name');

expect(BoardMock).toHaveBeenCalledTimes(1);
expect(UserMock).toHaveBeenCalledWith('some name');
expect(userMock.pickCards).toHaveBeenCalledTimes(1);
});
});

您可以在运行所有测试用例后重置所有模拟。

单元测试结果:

 PASS  stackoverflow/62199135/Game.test.jsx (10.16s)
62199135
✓ should pass (33ms)

----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
Game.jsx | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 11.474s, estimated 12s

关于reactjs - spy 模块和模拟模块功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62199135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com