标签: nodeunit

使用Sinon时,如何在存根实例中替换存根函数?

如果我已经创建了一个实例 var a = sinon.createStubInstance(MyContructor).

我怎样才能替换其中一个存根函数var stub = sinon.stub(object, "method", func);.

我这样做的主要原因是希望实现多个回调解决方法,如上所述

mocha.js node.js sinon nodeunit

13
推荐指数
2
解决办法
2万
查看次数

找不到Nodeunit命令?

我在Windows 7上运行并且使用cygwin安装了节点.我正在关注如何在mcmahon的网站上找到nodeunit:http://caolanmcmahon.com/posts/unit_testing_in_node_js .

我使用npm来安装nodeunit,它说它安装得很好但是当我去调用它时,nodeunit,它说在所有不同的目录中找不到命令.

我是否必须设置某种路径变量?

javascript cygwin command node.js nodeunit

11
推荐指数
2
解决办法
4239
查看次数

如何使用node-inspector调试nodeunit

我可以:

  • 我可以使用nodeunit测试node.js模块.
  • 我可以使用节点检查器调试我的node.js express站点.

但是如何使用节点检查器调试nodeunit测试?

我试过了,但没有工作:

  • nodeunit --debug myNodeUnitModule_test.js 它不起作用.
  • 我试着安装nodebug.并且像这样使用它:nodebug /usr/local/bin/nodeunit myNodeunit_test.js但是它既不在ubuntu(No such file or directory)也不在mac(env: node\r: No such file or directory)上工作

几乎工作 节点--debug/usr/local/bin/nodeunit ./routes/edit/bodyTelInfoArraysToObject_test.js

其中/usr/local/bin/nodeunit是命令采用的路径which nodeunit

获得输出: debugger listening on port 5858 并在那里执行测试.

但我不能跳进debuggin:当我localhost:8080在chrome中打开url 来观看调试时:

  1. 第一次加载我看到空文件列表
  2. 第二次加载:找不到页面.

在我的nodeunit测试中,我写debugger了停止调试那里.但没什么.

node.js nodeunit node-inspector

11
推荐指数
1
解决办法
2518
查看次数

Nodeunit test.throws似乎没有发现错误

我正在尝试使用Nodeunit为我在Node.js中编写的模块创建一个测试套件.该模块是一个基本的音乐播放列表,允许添加和删除播放列表中的曲目.

var playlist = function(){
    this.__playlist = [];
    this.__count = 0;
};

playlist.prototype = {
    addtrack:function(track){
        if(typeof track !== "object") throw new Error("Track needs to be an oject");
        this.__count++;
        track.id = this.__count;
        this.__playlist.push(track);
        return this.__playlist;
    },
    removetrack:function(trackid){
        if(typeof trackid !== "number") throw new Error("Pass in a numeric track id");
        var trackFound = false;
        for(var i=0;i<this.__playlist.length;i++){
            var t = this.__playlist[i];
            if(t.id == trackid){
                trackFound = true;
                this.__playlist.splice(i,1);
            }
        }
        if(!trackFound) throw new Error("Track not found in the playlist");
        return this.__playlist
    } …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing node.js nodeunit

9
推荐指数
1
解决办法
3377
查看次数

如何测试Grunt任务?理解和最佳实践

我有点不了解如何编写复杂的Gruntfile.js并将其与测试一起使用.我是否以正确的方式使用Grunt?我想请社区寻求帮助,并以其他方式做出贡献.

我正在为Grunt 写一个新任务,并希望在Github和npm上为广大观众推出它.我想为这项任务进行自动化测试(我想学习如何正确地完成它!).

我想测试不同的选项组合(现在大约15个).所以,我应该多次:

  • 运行清理
  • 使用下一个选项集运行我的任务
  • 运行测试并将选项对象传递给测试

一些非工作代码,以便更好地理解:

Gruntfile:

grunt.initConfig({

    test_my_task: {
        testBasic: {
            options: {
                 //first set
            }
        },
        testIgnore: {
            options: {
                //another set
            }
        },

        //...
    }

    clean: {
        tests: ['tmp'] // mmm... clean test directory
    },

    // mmm... unit tests.
    nodeunit: {
        tests: ['test/*.js']  //tests code is in 'tests/' dir
    }
});

grunt.registerTask('test', ['test_my_task']);
Run Code Online (Sandbox Code Playgroud)

我知道如何tmp/options给定对象时检查文件夹是否处于所需状态.

问题在于把事情放在一起.

我会问模板代码作为答案,npo需要放置工作示例.

PS:你可以提出另一个测试工具,nodeunit不是必须的.

PPS:垃圾,我现在可以用普通的javascript写这个!也许我错了,我想把Grunt放进单元测试中?但我想测试我的任务在真实环境中如何工作,从Grunt传递不同的选项......

unit-testing node.js nodeunit gruntjs

9
推荐指数
1
解决办法
4622
查看次数

用于使用mongoose在node.js上进行单元测试的结构

几个月来我一直在使用node.js进行开发,但现在我正在开始一个新项目,我想知道如何构建应用程序.

在谈论单元测试时,我的问题出现了.我将使用nodeunit编写单元测试.

我也使用express来定义我的REST路由.

我正在考虑编写我的代码,用两个"单独"的文件访问数据库(显然,它们会更多,但我只是想简化代码).将有路线代码.

var mongoose = require('mongoose')
 , itemsService = require('./../../lib/services/items-service');

// GET '/items'
exports.list = function(req, res) {
    itemsService.findAll({
        start: req.query.start,
        size: req.query.size,
        cb: function(offers) {
            res.json(offers);
        }
   });  
  };
Run Code Online (Sandbox Code Playgroud)

并且,正如我在那里使用的那样,项目服务仅用于访问数据层.我这样做是为了测试单元测试中的数据访问层.它会是这样的:

var mongoose = require('mongoose')
  , Item = require('./../mongoose-models').Item;

exports.findAll = function(options) {
    var query = Offer
        .find({});
    if (options.start && options.size) {
        query
            .limit(size)
            .skip(start)
    }
    query.exec(function(err, offers) {
        if (!err) {
                options.cb(offers);
            }
    })
};
Run Code Online (Sandbox Code Playgroud)

这样我可以检查单元测试它是否正常工作,我可以在任何我想要的地方使用这个代码.我唯一不确定它是否正确完成的是我传递回调函数以使用返回值的方式.

你怎么看?

谢谢!

javascript node.js nodeunit

8
推荐指数
1
解决办法
4283
查看次数

Nodeunit:测试函数中的运行时/抛出错误是_silent_

使用NodeUnit的一个要点是编写新函数并经常测试它们.问题是,如果其中一个测试函数抛出错误(包括JS运行时错误),则不会向用户显示错误.这是最简单的测试用例:(注意abcd会导致运行时错误)

exports.all = {
  one: function( test ){
    test.done();
  },

  two: function( test ){
    as( function( err, res ){
      test.done();
    });
  },
}


function as( callback ){
  process.nextTick( function() {
    a = testMe();
    callback( null, a );
  });
}

function testMe(){
  a.b.c.d.e = 100;
  return 10;
}
Run Code Online (Sandbox Code Playgroud)

但是,testMe()可能是我正在开发的新功能.一个未初始化的变量,或任何东西,只会沉默.

unit-testing mongodb node.js nodeunit

8
推荐指数
1
解决办法
871
查看次数

如何将node.js模块'nodeunit'与coffeescript文件一起使用

我正在尝试让nodeunit模块在coffeescript项目中运行,但似乎无法运行基本测试.这是我的例子Coffeescript要求'nodeunit'

test = true
test2 = false

exports.testSomething = (test) ->
  test.expect(1)
  test.ok(true, "this should pass")
  test.done()

exports.testSomethingElse = (test2) ->
  test2.expect(1)
  test2.ok(false, "this should fail")
  test2.done()
Run Code Online (Sandbox Code Playgroud)

不幸的是,当我运行'$ nodeunit example.coffee'时,我收到错误输出:

example.coffee:4 exports.testSomething =(test) - > ^

module.js:296 throw err; ^ SyntaxError:在Function._load上的Module.load(module.js:334:31)处的Object..js(module.js:408:10)处的Module._compile(module.js:397:25)处的意外的令牌> (module.js:293:12)在/ usr/local/lib/node的/usr/local/lib/node/nodeunit/lib/nodeunit.js:75:37上的require(module.js:346:19) /nodeunit/deps/async.js:508:13 /usr/local/lib/node/nodeunit/deps/async.js:118:13 /usr/local/lib/node/nodeunit/deps/async.js :134:9 /usr/local/lib/node/nodeunit/deps/async.js:507:9

任何人都可以帮助我使用Node.js在Coffeescript中进行简化测试并运行吗?

提前致谢

node.js coffeescript nodeunit

7
推荐指数
1
解决办法
1775
查看次数

node.js的断言库?

node.js断言为单元测试提供的断言非常有限.甚至在我编写第一个测试之前,我已经创建了一些断言,因为很明显我会继续重复使用它们.

你能推荐一些好的断言库来测试常见的javascript情况(对象结构,对象类等等)吗?

理想情况下,它应该与nodeunit很好地集成(或者,更好地,扩展它的断言) - 我的断言没有,我必须将它们test作为额外的变量传递...

我见过的唯一一个是.你能说些什么呢?

unit-testing assert node.js nodeunit chai

6
推荐指数
2
解决办法
4085
查看次数

从一个nodeunit文件中按名称运行一个测试

是否可以通过多次测试从nodeunit测试文件中仅运行一个测试.我的tests.js档案:

module.exports = {
    test2: function (test) {
        console.log ("test2")
        test.done();
    },
    test1: function (test) {
        console.log ("test1")
        test.done();
    }
};
Run Code Online (Sandbox Code Playgroud)

我可以运行所有测试:

$ nodeunit tests.js
Run Code Online (Sandbox Code Playgroud)

但是我想只运行test2:

$ nodeunit tests.js test2
Run Code Online (Sandbox Code Playgroud)

是否可以不将文件tests.js拆分为2个单独的文件?

nodeunit

6
推荐指数
1
解决办法
1444
查看次数