首先我要讓知道,我是新來的測試與反應,並希望確認,如果我寫正確的測試。被內部調用模擬功能反應成分渲染功能
我有一個陣營組件是這樣的 -
import React, { Component } from "react";
class DemoComponent extends Component
{
returnSomething()
{
return "something";
}
render(){
return(
<div>{ this.returnSomething() }</div>
);
}
}
和我寫測試,以驗證方法調用是這樣的 -
import React from "react";
import { shallow } from "enzyme";
import DemoComponent from "./DemoComponent.js";
const component = shallow(<DemoComponent/>);
test('"returnSomething" method is called when the component is rendered',() => {
component.instance().returnSomething= jest.fn();
component.update();
component.instance().render();
expect(component.instance().returnSomething).toHaveBeenCalled();
});
測試運行正常,但我想知道如果我寫的測試是正確的方法。
你能解釋一下你爲什麼要考這樣的組件,而不是使用酶和快照? https://medium.com/@gethylgeorge/testing-a-react-redux-app-using-jest-and-enzyme-b349324803a9 –
我使用'enzyme'。我只發佈了一部分代碼。可能是爲了方便,我應該更新代碼。 @AndreasKöberle – besrabasant
爲什麼你不在測試中使用它? 'expect(component).toMatchSnapshot()'會測試渲染方法的結果。您是否知道快照功能? –