简体   繁体   English

如何模拟我的配置文件进行测试?

[英]How do I mock my config file for testing?

I have a Koa app I just started and I need to test something that grabs data from a config file.我有一个刚刚启动的 Koa 应用程序,我需要测试一些从配置文件中获取数据的东西。

I need to test with specific data, but I'm not sure how to modify what data the test receives from the config file.我需要使用特定数据进行测试,但我不确定如何修改测试从配置文件接收的数据。

Example:例子:

app.js应用程序.js

var router = require('koa-router');
var config = require('./config.js');
var db     = require('./db.js');
var auth   = require('./auth');
var app    = require('koa')();

router.get('/', function *() {
  if(auth(this.req, config.credentials.secret)) { // Authenticates request based on a hash created using a shared secret
    this.body = "Request has been authenticated";
  }
});

app.use(router.routes());
app = module.exports = http.createServer(app.callback());

app.listen(3000);

appSpec.js appSpec.js

var request = require('supertest');
var app = require('../app.js');

describe('app', function() {
  it('should authenticate all requests against config shared secret', function() {
    var secret    = 'some_secret';
    var validHash = /* hash created from test secret and query */;

    request(app)
      .get('/')
      .query({query: 'some_query'})
      .query({hash: validHash})
      .expect(403, done);

  });
});

This spec will fail because the app will use the secret from the config file(empty string) instead of my test secret.此规范将失败,因为应用程序将使用配置文件(空字符串)中的机密而不是我的测试机密。

Alright, I played around with some different ways to handle this.好吧,我尝试了一些不同的方法来处理这个问题。

The best option I found, for my particular use case, was proxyquire .对于我的特定用例,我发现的最佳选择是proxyquire It's an npm package that lets you override dependencies in any file that you require in your test suites.它是一个 npm 包,可让您覆盖测试套件中所需的任何文件中的依赖项。

So if I am testing this module:因此,如果我正在测试此模块:

./controllers/myController.js ./controllers/myController.js

var config = require('../config.js');

module.exports = function() {
 // Do some stuff
};

I would do something like this:我会做这样的事情:

./test/controllers/myControllerSpec.js ./test/controllers/myControllerSpec.js

var proxyquire = require('proxyquire');
var config = {
  credentials: {
    secret: 'my_secret'
  }
  // other fake config stuff
};
var myController = proxyquire('../../controllers/myController.js', {'../config.js', config});

describe('myController', function() {
  // TESTS
});

and this instance of myController will use my test config.这个myController实例将使用我的测试配置。

This won't work for end to end testing, unless the only place you import your config is the main app file.这不适用于端到端测试,除非您导入配置的唯一位置是主应用程序文件。

I use node-config for my config files and configuration loading based on machine or env variable.我将node-config用于我的配置文件和基于机器或环境变量的配置加载。

You can specify your config in a variety of formats (.json, .js, yaml, etc.) Using the default settings, you need to create a config folder in your app root and a default.<format> with your default config.您可以以多种格式(.json、.js、yaml 等)指定您的配置。使用默认设置,您需要在您的应用程序根目录中创建一个config文件夹,并使用您的默认配置创建一个default.<format>

To override that for testing you can create a test.<format> file in your config directory.要覆盖它以进行测试,您可以在config目录中创建一个test.<format>文件。 When you set your NODE_ENV=test , then node-config will see load your default config file and then it will load your test config file and if there are any conflicts for the values, your test config file will override the values in your default file.当您设置NODE_ENV=testnode-config将看到加载您的默认配置文件,然后它将加载您的测试配置文件,如果这些值存在任何冲突,您的测试配置文件将覆盖默认文件中的值.

Here are the full docs for setting up Configuration Files in node-config以下是在node-config设置配置文件的完整文档

Below is an example using node-config with a .js config file format.下面是一个使用node-config.js配置文件格式的示例。

./config/default.js ./config/default.js

module.exports = {
  credentials: {
    secret: ''
  }
}

./config/test.js ./config/test.js

module.exports = {
  credentials: {
    secret: 'abcdef123456'
  }
}

app.js应用程序.js

var router = require('koa-router');
var config = require('config');
var db     = require('./db.js');
var auth   = require('./auth');
var app    = require('koa')();

var credentials = config.get('credentials');

router.get('/', function *() {
  if(auth(this.req, credentials.secret)) { // Authenticates request based on a hash created using a shared secret
    this.body = "Request has been authenticated";
  }
});

app.use(router.routes());
app = module.exports = http.createServer(app.callback());

app.listen(3000);

Jest has nice support for this case. Jest 对这种情况有很好的支持。 In your test file, add在您的测试文件中,添加

jest.mock('../config.js', () => ({ 
  credentials: {
    secret: 'my_secret'
  }
  // other fake config stuff }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模拟json文件如何将其包含在项目中? - Mock json file how do I include it in my project? 在测试AngularJS控制器时,如何在$ http.get承诺中模拟结果? - How do I mock the result in a $http.get promise when testing my AngularJS controller? 如何在 vs 代码中更新我的配置文件? - How do I update my config file in vs code? 你如何在浏览器中模拟文件选择器进行单元测试? - How do you mock out the File Picker in the browser for unit testing? 我如何在 Jasmine 中模拟单元测试的全局变量? - How do i mock a global variable for Unit Testing in Jasmine? 如何在服务人员中获取我的配置 json 文件 - How do I fetch my config json file inside service worker 如何模拟或伪造网站的行为以允许对某些JS功能进行单元测试 - How do I mock or fake behavior of a website to allow unit testing for certain JS functions 在对 class 方法进行单元测试时,我如何模拟它调用的 function - 在导入的模块中定义? - When unit testing a class method, how do I mock a function it calls--one defined in an imported module? 如何将调试添加到我的 webpack 配置中 - how do I add debug to my webpack config 如何在 *Windows 10* 的 Jest 配置中设置时区? - How do I set a timezone in my Jest config in *Windows 10*?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM