英文:
Test data endpoints
问题
The error you're encountering is due to a typo in your test code. In your test case, you are expecting the result to be "76915222"
:
expect(result?.map((total) => total.total_buildings)).toBe("76915222");
However, in your actual code, the property is named total_building
, not total_buildings
:
data: [
{
total_building: "76915222",
},
],
To fix the error, you should update your test expectation to match the correct property name:
expect(result?.map((total) => total.total_building)).toBe("76915222");
This should resolve the issue when you run npm run test
.
英文:
Here I want to test the data output on the endpoint.
My test:
import { render, screen } from "@testing-library/react";
import axios from "axios";
import CompBuildingGeo from "./components/CompBuildingGeo";
import { getBuildingGEO } from "../../service/building";
const buildingGeo = async () => {
try {
const result = await getBuildingGEO();
console.log(result?.map((total) => total.total_building));
} catch (error) {
console.log("salah");
} finally {
console.log("none");
}
};
describe("Component Building", () => {
test("CompBuildingGeo API", async () => {
render(<CompBuildingGeo />);
jest.spyOn(axios, "get").mockReturnValue({
data: [
{
total_building: "76915222",
},
],
});
const result = await buildingGeo();
expect(result).toBe("76915222");
});
});
However, when I npm run test
, this error appears:
Expected: "76915222"
Received: undefined
35 | });
36 | const result = await buildingGeo();
> 37 | expect(result?.map((total) => total.total_buildings)).toBe("76915222");
|
38 | });
39 |});
What is the cause of the error?
答案1
得分: 2
Add a return
statement to the buildingGeo
function, like this:
const buildingGeo = async () => {
try {
const result = await getBuildingGEO();
console.log(result?.map((total) => total.total_building));
return result;
} catch (error) {
console.log("salah");
} finally {
console.log("none");
}
};
You may need to access the data
property before returning (result.data
), but the provided code should work. The undefined
issue was likely because your function wasn't returning anything.
英文:
just add a return
statement to buildingGeo
function, so instead of:
try {
const result = await getBuildingGEO();
console.log(result?.map((total) => total.total_building));
} catch (error) {
console.log("salah");
} finally {
console.log("none");
}
};
It will be:
const buildingGeo = async () => {
try {
const result = await getBuildingGEO();
console.log(result?.map((total) => total.total_building));
return result;
} catch (error) {
console.log("salah");
} finally {
console.log("none");
}
};
I'm not 100% percent sure, but you might have to access the data
property before returning (result.data
), but the code above should work.
The undefined
you were getting was happening because your function was not returning anything.
答案2
得分: -2
我编辑了这部分 =>
const buildingGeo = async () => {
try {
const result = await axios.get(
`${process.env.REACT_APP_SERVICE_DISTRICT}/buildingCountGeoSettleAll`,
{
headers: { Authorization: `Bearer ${token}` },
}
);
return result;
} catch (error) {
console.log("错误");
} finally {
console.log("无论如何");
}
};
和工作。
英文:
I edited this part =>
const buildingGeo = async () => {
try {
const result = await axios.get(
`${process.env.REACT_APP_SERVICE_DISTRICT}/buildingCountGeoSettleAll`,
{
headers: { Authorization: `Bearer ${token}` },
}
);
return result;
} catch (error) {
console.log("salah");
} finally {
console.log("none");
}
};
and work.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论