使用Bookshelf.js和knex.js进行单元测试 [英] Unit testing with Bookshelf.js and knex.js

查看:96
本文介绍了使用Bookshelf.js和knex.js进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Node相对较新,并且正在使用knex和书架进行项目.我在对代码进行单元测试时遇到了一些麻烦,但是我不确定自己做错了什么.

I'm relatively new to Node and am working on a project using knex and bookshelf. I'm having a little bit of trouble unit testing my code and I'm not sure what I'm doing wrong.

基本上,我有一个看起来像这样的模型(称为VorcuProduct):

Basically I have a model (called VorcuProduct) that looks like this:

var VorcuProduct = bs.Model.extend({
    tableName: 'vorcu_products'
});

module.exports.VorcuProduct = VorcuProduct

还有一个函数,如果数据库中不存在VorcuProduct,则该函数将保存该VorcuProduct.非常简单.执行此操作的函数如下所示:

And a function that saves a VorcuProduct if it does not exist on the DB. Quite simple. The function doing this looks like this:

function subscribeToUpdates(productInformation, callback) {
  model.VorcuProduct
    .where({product_id: productInformation.product_id, store_id: productInformation.store_id})
    .fetch()
    .then(function(existing_model) {
        if (existing_model == undefined) {
            new model.VorcuProduct(productInformation)
                .save()
                .then(function(new_model) { callback(null, new_model)})
                .catch(callback);
        } else {
            callback(null, existing_model)
        }
    })
}

哪种方法在不影响数据库的情况下进行测试是正确的?我是否需要模拟fetch以返回模型或未定义(取决于测试),然后对save执行相同操作?我应该为此使用重布线吗?

Which is the correct way to test this without hitting the DB? Do I need to mock fetch to return a model or undefined (depending on the test) and then do the same with save? Should I use rewire for this?

如您所见,我有点迷失了,所以我们将不胜感激.

As you can see I'm a little bit lost, so any help will be appreciated.

谢谢!

推荐答案

我一直在使用内存中Sqlite3数据库,用于自动化测试,取得了巨大的成功.我的测试需要10到15分钟才能在MySQL上运行,而使用内存中的sqlite3数据库则只需30秒左右.使用:memory:作为连接字符串可利用此技术.

I have been using in-memory Sqlite3 databases for automated testing with great success. My tests take 10 to 15 minutes to run against MySQL, but only 30 seconds or so with an in-memory sqlite3 database. Use :memory: for your connection string to utilize this technique.

有关单元测试的说明-这不是真正的单元测试,因为我们仍在对数据库运行查询.这是技术上的集成测试,但是它在合理的时间内运行,并且如果您有大量查询的应用程序(例如我的应用程序),那么与任何单元测试相比,该技术将比其他单元测试更有效地捕获错误.

A note about unit tesing - This is not true unit testing, since we're still running a query against a database. This is technically integration testing, however it runs within a reasonable time period and if you have a query-heavy application (like mine) then this technique is going to prove more effective at catching bugs than unit testing anyway.

Gotchas -Knex/Bookshelf在应用程序启动时初始化连接,这意味着您可以在测试之间保留上下文.我建议编写一个模式创建/销毁脚本,以便您建立和销毁每个测试的表.另外,Sqlite3对外键约束的敏感度不及MySQL或PostgreSQL,因此请确保您不时针对其中之一运行应用程序,以确保约束条件可以正常工作.

Gotchas - Knex/Bookshelf initializes the connection at the start of the application, which means that you keep the context between tests. I would recommend writing a schema create/destroy script so that you and build and destroy the tables for each test. Also, Sqlite3 is less sensitive about foreign key constraints than MySQL or PostgreSQL, so make sure you run your app against one of those every now and then to ensure that your constraints will work properly.

这篇关于使用Bookshelf.js和knex.js进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆