[英]proxyquire Error: ENOENT: no such file or directory, scandir
Hello I am new to testing with mocha/chai/sinon and sequelize-test-helpers
您好,我是使用
mocha/chai/sinon and sequelize-test-helpers
Trying to use proxyquire
to override the require
but having issues尝试使用
proxyquire
覆盖require
但有问题
Getting this following error about the path:收到有关路径的以下错误:
Error: ENOENT: no such file or directory, scandir 'C:<local-directories-path>\ecommerce-pern-app\server\src\models'
I dont get why there is a src folder
when I don't have a src folder
at all I am using the proxyquire
in the test file and its path is from the server
directory would be:我不明白为什么有一个
src folder
时,我没有src folder
的所有我使用的proxyquire
在测试文件及其路径是从server
目录将是:
server/specs/services/user-service.spec.js服务器/规格/服务/用户服务.spec.js
"use strict";
const chai = require('chai');
const {match, stub, resetHistory, spy} = require('sinon');
const proxyquire = require('proxyquire');
const path = require('path');
const service = path.resolve('./services/userService.js')
var sinonChai = require("sinon-chai");
chai.should();
chai.use(sinonChai);
console.log(service)
const {makeMockModels, sequelize, dataTypes,} = require('sequelize-test-helpers');
describe('Idea Controller', function () {
const uid = '6a88e9b5-33a2-403f-ac3d-e86413ac101d'
const data = {
email: 'testface@test.com',
password: '123456',
is_admin: false,
first_name: 'Testy',
last_name: 'McTestface',
google_id: null,
facebook_id: null
}
describe('findAll()', function () {
it('Success case ', function () {
const mockResponse = () => {
const res = {};
res.json = stub().returns(res);
return res;
};
let res = mockResponse();
const User = {findAll: stub()};
const mockModels = makeMockModels({User});
Idea.findAll.resolves(data);
const UserService = proxyquire(service, {
"save": {}
});
UserService.findAll({}, res);
Idea.findAll.should.have.been.called; // passes
res.json.should.have.been.called; //fails
});
})
});
In the above code I am using the proxyquire like this:在上面的代码中,我使用的是这样的 proxyquire:
const proxyquire = require('proxyquire');
const path = require('path');
const service = path.resolve('./services/userService.js')
const {makeMockModel} = require('sequelize-test-helpers');
const mockModels = makeMockModels({User});
const UserService = proxyquire(service, {
"../models": mockModels
});
As I am trying to use the path to find the server/service/userService.js
file which is relatively located from the test file at ../../services/userService.js
.因为我正在尝试使用路径来查找
server/service/userService.js
文件,该文件位于../../services/userService.js
的测试文件中。 I have got this bug that there is src folder
there when I do not have a src directory
at all even!我有这个错误,当我根本没有
src directory
时,那里有src folder
!
As the bug is saying:正如错误所说:
Error: ENOENT: no such file or directory, scandir 'C:<local-directories-path>\ecommerce-pern-app\server\src\models'
Whatever I try about file path is not working I tried path.resolve, path.join and directly typing the path into it as like ../../services/userService.js
无论我尝试关于文件路径的尝试都不起作用,我尝试了 path.resolve、path.join 并直接将路径输入到其中,例如
../../services/userService.js
here is the server/services/userService.js这是服务器/服务/userService.js
const Models = require('../models');
const { User } = Models;
const save = async ({ id, ...data }) => {
const user = await User.findOne({ where: { uid: id } })
if (user) return await user.update(data)
return null
}
module.exports = save;
I just want the path to with proxyquire to work我只希望使用 proxyquire 的路径起作用
What is this \\src\\models path from the error, I dont have a src/models path route at all!错误中的 \\src\\models 路径是什么,我根本没有 src/models 路径!
This is a quote from sequelize-test-helpers's readme .这是引自sequelize-test-helpers 的自述文件。
As a convenience, makeMockModels will automatically populate your mockModels with mocks of all of the models defined in your src/models folder (or if you have a .sequelizerc file it will look for the models-path in that).
为方便起见,makeMockModels 将自动使用src/models 文件夹中定义的所有模型的模拟填充您的模拟模型(或者如果您有一个 .sequelizerc 文件,它将在其中查找模型路径)。 Simply override any of the specific models you need to do stuff with.
只需覆盖您需要处理的任何特定模型。
So you need to provide .sequelizerc file and define models-path
.所以你需要提供 .sequelizerc 文件并定义
models-path
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.