我正在编写一个React-Native应用程序,我需要测试一个屏幕:
import requests # to make requests.
import pdfkit # for saving as pdf
url="http://exam.cusat.ac.in/erp5/cusat/CUSAT-RESULT/Result_Declaration/display_sup_result" #form url
pdfs=[]
payload={ "deg_name":"B.Tech", "semester":"5", "month":"November", "year":"2016", "result_type":"Regular" }
option={'quiet': ''}
for i in range(12153600,12153660+1):
payload.update({"regno":str(i)})
response=requests.post(url,data=payload)
pdfkit.from_string(response.content,str(i)+".pdf",options=option) #saves to 12153600.pdf - 12153660.pdf files
open("result_"+str(i)+".html","w").write(response.content) #This will save results from roll no 12153600 - 12153660 in result_rollno.html files.
pdfkit
MyScreen.js
import React, { Component } from "react";
import CustomTable from "./CustomTable";
export default MyScreen extends Component {
render() {
return <CustomTable />;
}
}
CustomTable.ios.js
调用一些特定于iOS的代码,所以我通过简单地在import React, { Component } from "react";
import { View } from "react-native";
import TableView from "react-native-tableview";
export default MyScreen extends Component {
render() {
return (
<View>
...some stuff
<TableView />
</View>
);
}
}
文件夹中返回android版本(react-native-tableview
)来嘲笑它
CustomTable.android.js
__mocks__
我想要做的是使用Jest测试__mocks__/CustomTable.ios.js
,但我希望它使用import CustomAndroidTable from "../CustomTable.android";
export const CustomTable = CustomAndroidTable;
。我该怎么做才能做到这一点?这对Jest来说甚至可能吗?我当前的测试文件如下:
MyScreen.js
__mock__/CustomTable.ios
但它仍然会调用tests/MyScreen.test.js
的原始版本。我做错了什么?
答案 0 :(得分:3)
您应该在套件测试之外调用jest.mock。它应该在你的进口后立即。
import React from "react";
import renderer from "react-test-renderer";
import MyScreen from "../src/MyScreen";
jest.mock("../src/CustomTable.ios");
describe("test", () => {
it("works", () => {
const tree = renderer.create(
<MyScreen />,
).toJSON();
expect(tree).toMatchSnapshot();
});