javascript - 尝试模拟 const 在 nodejs 中不起作用
问题描述
我一直在尝试在 movie.js 中模拟 fetchFromCache 方法,但到目前为止还不可能。我尝试了很多东西,但我认为编写方式使一切变得更加复杂。我无法更改代码,因为有很多东西是这样写的,我现在正在添加单元测试。我尝试的最后一件事是这样:
jest.mock('../src/movie', () => ({
fetchFromCache: 'number fake'
}));
fetchMovieHandler.js
const Movie = require('./movie');
exports.handler = async function (event) {
let movieId = event.movieId;
return Movie.getMovieId(movieId);
};
movie.js
const getMovieId = movieId =>
Promise.resolve(fetch(movieId))
const fetchMovieFromCache = movieId => {
return fetchFromCache(movieId)
}
const fetchFromCache = movieId => new Promise((resolve, reject) => {
cacheClient.get(movieId,
(err, data) => {
if(data) {
return resolve(data);
} else {
return resolve(null);
}});
});
Testing
const fetchMovieHandler = require("../src/fetchMovieHandler");
describe('Movie Id', () => {
test("movie id valid", () => {
return fetchMovieHandler.handler(event("123")).then(value => {
expect(value).toBe("value returned");
})
});
})
const event = (movieId) => {
return {
"movieId": movieId
};
}
得到
Movie.getMovieId is not a function
任何想法?
解决方案
推荐阅读
- flutter - 如何解决 Flutter 中 ListView.Builder 小部件中的“RenderBox 未布局:”?
- r - R如何将多面体合并到多边形
- c# - 具有对象类型的 C# 方法 Object.Equals (object a, object b) 返回错误的结果
- jenkins - 使用 Jenkins 时如何更新 gitlab Merge Request 构建状态(管道状态)并且管道先前已取消/删除?
- django - 多对多关系字段的django rest框架列表
- php - 将成功消息传输到 ajax 中的重定向主页(本机 JS/非 JQUERY)
- reactjs - 如何减小 antdesign.less 文件的包大小?
- browser - 如何解决我的网站和同一域的网站之间的这个 CORS 问题?
- java - 如何在 pom.xml 中配置 projectId 使用两个项目时 google cloud
- python - 任何停止在excel中自动重新格式化数据的方法