如何在Node.js中模拟MySQL(无ORM)? [英] How do you mock MySQL (without an ORM) in Node.js?

查看:92
本文介绍了如何在Node.js中模拟MySQL(无ORM)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将Node.js与felixge的 node-mysql 客户端一起使用.我没有使用ORM.

I'm using Node.js with felixge's node-mysql client. I am not using an ORM.

我正在用Vows进行测试,并希望能够模拟我的数据库(可能使用Sinon).由于我本身并没有DAL(除了node-mysql之外),因此我不确定如何执行此操作.我的模型大多是带有很多吸气剂的简单CRUD.

I'm testing with Vows and want to be able to mock my database, possibly using Sinon. Since I don't really have a DAL per se (aside from node-mysql), I'm not really sure how to go about this. My models are mostly simple CRUD with a lot of getters.

关于如何实现此目标的任何想法?

Any ideas on how to accomplish this?

推荐答案

使用sinon,您可以在整个模块周围放置模拟或存根.例如,假设mysql模块具有功能query:

With sinon, you can put a mock or stub around an entire module. For example, suppose the mysql module has a function query:

var mock;

mock = sinon.mock(require('mysql'))
mock.expects('query').with(queryString, queryParams).yields(null, rows);

queryStringqueryParams是您期望的输入. rows是您期望的输出.

queryString, queryParams are the input you expect. rows is the output you expect.

当您正在测试的类现在需要mysql并调用query方法时,它将被sinon拦截并验证.

When your class under test now require mysql and calls the query method, it will be intercepted and verified by sinon.

在测试期望部分中,您应该具有:

In your test expectation section you should have:

mock.verify()

在拆解中,您应该将mysql恢复到正常功能:

and in your teardown you should restore mysql back to normal functionality:

mock.restore()

这篇关于如何在Node.js中模拟MySQL(无ORM)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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