Tag: 摩卡

用sin子嘲笑/ st住mongoosefindById

我试图把我的mongoose模型,特别是mongoose的findById方法 我试图让mongoose返回指定的数据,当调用findById'abc123' 以下是我到目前为止: require('../../model/account'); sinon = require('sinon'), mongoose = require('mongoose'), accountStub = sinon.stub(mongoose.model('Account').prototype, 'findById'); controller = require('../../controllers/account'); describe('Account Controller', function() { beforeEach(function(){ accountStub.withArgs('abc123') .returns({'_id': 'abc123', 'name': 'Account Name'}); }); describe('account id supplied in querystring', function(){ it('should retrieve acconunt and return to view', function(){ var req = {query: {accountId: 'abc123'}}; var res = {render: function(){}}; controller.index(req, […]

代码覆盖多个文件

我在使用摩卡框架的node.js中有一个应用程序。 我有两个JavaScript源文件,我想要代码覆盖(即a.js和b.js)。 为此,我正在使用伊斯坦布尔。 这里的问题是我没有得到如何采取代码覆盖多个文件。 我正在使用以下格式: istanbul cover node_modules/mocha/bin/_mocha a.js istanbul cover node_modules/mocha/bin/_mocha a.js b.js 但不幸的是,两个命令都给出了相同的代码覆盖率,我认为它只带了一个.js代码。 有没有find多个文件的代码覆盖率的解决scheme?

用摩卡testingSails.js只运行一个testing不是两个

我遵循http://sailsjs.org/#!/documentation/concepts/Testing中的sails.jstesting示例。 我已经得到它运行,但它只运行一个testing。 package.json script.test中的命令行是: 摩卡testing/ bootstrap.test.jstesting/单元/ ** / *。test.js “test / unit / ** / *。test.js”应该捕获2个testing,UsersControllers.test.js和Users.test.js。 它只运行Users.test.js。 是的,两个testing都在testing/单元/目录中。 我在这里做错了什么?

我怎样才能得到摩卡独立运行我的testing?

我正在使用摩卡来运行使用节点编写的testing。 我的假设是,我的每个testing都会被隔离开来。 这似乎并非如此。 在testing目录上运行mocha时,它似乎将所有testing文件加载在一起,然后执行每个testing套件。 当一个testing中使用模块时,这可能会中断隔离,这可能会受到另一个testing中使用的模块的影响。 在这个Gist( 失败的摩卡testing )中,我有两个模块(a和b)和两个testing(atesting和btesting)。 如果你在每次testing中独立运行摩卡,他们都会成功: $ mocha –ui tdd a-test $ mocha –ui tdd b-test 但是,如果我一起运行这些testing,atesting失败: $ mocha –ui tdd . 是否真的有必要为每个单独的testing运行摩卡以得到适当的隔离? 注意:atesting失败的原因是在b模块中触发一个调用单例的事件。 这在b检验的正常执行中不会发生。 由于atesting提供了一套完整的依赖关系(不包括b),所以我惊讶地发现所有的模块都被加载到一个testing环境中。

使用伊斯坦布尔对Node微服务进行集成testing

在进行集成testing的伊斯坦布尔覆盖面时,文档相当稀less。 当我通过我的摩卡testing时,我得到No coverage information was collected, exit without writing coverage information 。 我做的第一件事就是把我所有的源代码整理好: ✗ istanbul instrument . -o .instrument 就我而言,这是一个REST微服务,它是Docker化的,我已经写了Mochatesting来运行它来validation它,一旦它被部署。 我的期望是伊斯坦布尔会给我的代码覆盖从该节点服务的来源。 第二步我执行这个命令在我的testing代码上运行节点: ✗ istanbul cover –report none .instrument/server.js 之后,我运行我的testing使用以下从我的主要src目录如下(与结果): ✗ istanbul cover –report none –dir coverage/unit node_modules/.bin/_mocha — -R spec ./.instrument/test/** –recursive swagger-tests #createPet ✓ should add a new pet (15226ms) #getPets ✓ should exist […]

如何在Sails中使用摩卡testing控制器?

我有我的login页面下面的Controller : // Authentication Controller // the basics of Passport.js to work. var AuthController = { // localhost:1337/login Render the login page // <form role="form" action="/auth/local" method="post"> // <input type="text" name="identifier" placeholder="Username or Email"> // <input type="password" name="password" placeholder="Password"> // <button type="submit">Sign in</button> // </form> login: function(req, res) { var strategies = sails.config.passport, providers = […]

testingnodejs和声生成器方法

假设你有以下的JSfunction: function YourProxy($orm, $usr) { this.addToDB = function(obj) { /* Do some validation on obj */ return function(callback){ var oo = $orm.createNew(obj); oo.save(options, function(err,ok){ if(err) callback(err); callback(null,ok); } } } } 你可以在node.js上使用ES6生成器来等待这个操作发生,如下所示: function *(){ var yourProxy = new YourProxy(); try { var result = yield yourProxy.addToDB(anObject); } catch(e) { /* Something went wrong sync. Here […]

Node.js摩卡testingRestful API端点和代码覆盖率

我一直在享受伊斯坦布尔的乐趣,并尝试其他的Node.js覆盖库,但是我有一个问题。 几乎所有的unit testing都是对我的API的HTTP调用,如下所示: it('should update the customer', function (done) { superagent.put('http://myapp:3000/api/customer') .send(updatedData) .end(function (res) { var customer = res.body; expect(res.statusCode).to.equal(200); expect(customer.name).to.equal(updatedData.name); done(); }); }); 而不是实际需要customers.js文件并直接调用updateCustomer 。 testing端点对我来说更有意义,因为它不仅testingupdateCustomer ,而且还testing路由,控制器和其他所有参数。 这工作正常,但问题是,我似乎无法看到任何代码覆盖工具识别这些testing的方法。 伊斯坦布尔还有什么办法可以承认这些摩卡testing吗? 如果不是,那么约定是什么? 你如何testing端点,仍然使用代码覆盖工具?

Mocha如何知道在testing套件中首先加载哪个文件

我正试图用MongodB学习一个testing驱动的方法。 文件夹结构 一个user.js在src文件夹中testing const mongoose = require('mongoose'); mongoose.Promise = require('bluebird'); const Schema = mongoose.Schema; const UserSchema = new Schema ({ name: String }); const User = mongoose.model('user', UserSchema); module.exports = User; test_helper.js内容 const mongoose = require('mongoose');; mongoose.connect('mongodb://localhost/users_test'); mongoose.connection .once('open', () => { console.log('Connected to Mongo!'); done()}) .on('error', (error) => { console.warn('Warning', error); }); create_test.js内容 const […]

摩卡testing超时,如果超过4个testing一次运行

我有一个我正在用Mochatesting的node.js + express web服务器。 我在testing工具中启动Web服务器,并连接到MongoDB以查找输出: describe("Api", function() { before(function(done) { // start server using function exported from another js file // connect to mongo db }); after(function(done) { // shut down server // close mongo connection }); beforeEach(function(done) { // empty mongo collection }); describe("Events", function() { it("Test1", …); it("Test2", …); it("Test3", …); it("Test4", …); […]