我尝试在节点中使用测试工具mocha.请考虑以下测试方案
var requirejs = require('requirejs');
requirejs.config({
//Pass the top-level main.js/index.js require
//function to requirejs so that node modules
//are loaded relative to the top-level JS file.
nodeRequire: require
});
describe('Testing controller', function () {
it('Should be pass', function (done) {
(4).should.equal(4);
done();
});
it('Should avoid name king', function (done) {
requirejs(['../server/libs/validate_name'], function (validateName) {
var err_test, accountExists_test, notAllow_test, available_test;
validateName('anu', function (err, accountExists, notAllow, available) {
accountExists.should.not.be.true;
done();
});
});
});
});
Run Code Online (Sandbox Code Playgroud)
作为测试结果我有:
$ make test
./node_modules/.bin/mocha \
--reporter list …
Run Code Online (Sandbox Code Playgroud) 我的测试可以在没有Meteor运行的情况下存在吗?
我刚刚开始了我的第一个Meteor项目,并开始使用Mocha和should.js编写单元测试.虽然mocha运行没有问题,但测试阻止Meteor启动,因为它使用节点require
而不是__meteor_bootstrap__.require
(完整错误消息)有问题.
话虽这么说,Meteor不应该运行我的测试!根据Meteor文档,代码只能放在客户端,服务器或两者上.单元测试套件不属于这些类别,并且我不是唯一被Meteor缺乏定位自动化测试位置的人所困惑的人.
现在,我的测试保存在server/test/
,每个文件的内容都包含在块中:
if (typeof(Meteor) === 'undefined') { ... }
Run Code Online (Sandbox Code Playgroud)
虽然这有效,但感觉并不优雅.您对使用Meteor应用程序构建测试有任何其他建议吗?
更新:代替Meteor文档中的显式指令,我遵循Rails文件夹约定(第4段),使用一个名为test
存储我的测试资产的文件夹.后来server/test
我把它移到了,因为我不希望它加载到客户端上.
我有这样的单元测试:
(parsed.date).should.equal(new Date(2006,06,18,18,07));
Run Code Online (Sandbox Code Playgroud)
该消息失败了:
AssertionError: expected 2006-07-19T00:07:00.000Z to be 2006-07-19T00:07:00.000Z
+ expected - actual
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在使用mocha/supertest/should.js来测试我的Rest Service
GET /files/<hash>
将文件作为流返回.
如何在should.js中断言文件内容是否相同?
it('should return file as stream', function (done) {
var writeStream = fs.createWriteStream('test/fixtures/tmp.json');
var req = api.get('/files/676dfg1430af3595');
req.on('end', function(){
var tmpBuf = fs.readFileSync('test/fixtures/tmp.json');
var testBuf = fs.readFileSync('test/fixtures/test.json');
// How to assert with should.js file contents are the same (tmpBuf == testBuf )
// ...
done();
});
});
Run Code Online (Sandbox Code Playgroud) 我试图用should.js(最新版本)做一个deepEqual断言但没有取得任何成功.我可以让事情与之合作,equal
但不是deepEqual
.事实上,我发现没有deepEqual
方法.
这是我尝试过的:
> require('should')
{...}
> > var x = Number(8)
undefined
> x.should.equal(8)
{ obj: 8 }
> x.should.equal(9)
AssertionError: expected 8 to equal 9
at ....
> x.should.deepEqual(8)
TypeError: Object #<Object> has no method 'deepEqual'
Run Code Online (Sandbox Code Playgroud)
很公平.现在看should
,我看到它是一个吸气剂:
> Object.getOwnPropertyDescriptor(Object.prototype, 'should')
{ get: [Function],
set: [Function],
enumerable: false,
configurable: true }
Run Code Online (Sandbox Code Playgroud)
既然它是一个吸气剂,我该如何检查其键?这几乎有效:
> Object.keys(Object.prototype.should)
[ 'obj' ]
Run Code Online (Sandbox Code Playgroud)
但后来我明白了
> Object.getOwnPropertyDescriptor(should.obj)
{ value: undefined,
writable: false,
enumerable: false,
configurable: false }
Run Code Online (Sandbox Code Playgroud)
所以我现在很困惑.我想知道会发生什么事情should …
因此,我对mocha完全不熟悉,并且我被要求编写集成测试,将表单数据发布到页面,获取身份验证密钥,然后对密钥运行测试.
我正确地获得了密钥,这可以通过我的日志声明来确认,但是当我运行mocha时,我的测试表示0传递.它甚至没有打印出'describe'和'it'语句的描述,暗示它们在promise中不起作用.有没有我可以这样工作,同时仍然让断言访问我的授权密钥?
require('should');
require('sails');
var Promise = require("promise");
var request = require('request');
describe('Domain Model', function(){
var options = { url:'link',
form:{
username: 'xxxxx@xxxxxx',
password: '0000'
},
timeout: 2000
};
//request auth key
var promise = new Promise(function(resolve,reject){
request.post(options, function(error, response, body){
//body contains the auth key, within a string of JSON
resolve(body);
});
});
//this is where I'm getting lost
promise.then(function(data,test){
var token = (JSON.parse(data))["access_token"];
console.log(token);
describe('Initialize',function(){
it('should be an string', function(){
(token).should.be.type('string');
});
});
});
});
Run Code Online (Sandbox Code Playgroud) 我想做一个类似的简单断言
knownArray.should.include('known value')
Run Code Online (Sandbox Code Playgroud)
数组是正确的,但我根本无法弄清楚用于检查数组是否具有此值的正确断言(索引无关紧要).我也试过,should.contain
但这两个都抛出一个错误Object #<Assertion> has no method 'contain'
(或'include'
)
如何检查数组是否包含使用的元素should
?
我有一个文件app.coffee
:
class TaskList
class Task
constructor: (@name) ->
@status = 'incomplete'
complete: ->
if @parent? and @parent.status isnt 'completed'
throw "Dependent task '#{@parent.name}' is not completed."
@status = 'complete'
true
dependsOn: (@parent) ->
@parent.child = @
@status = 'dependent'
# Prepare scope stuff
root = exports ? window
root.TaskList = TaskList
root.Task = Task
Run Code Online (Sandbox Code Playgroud)
和一个名为的文件test/taskTest.coffee
:
{TaskList, Task} = require '../app'
should = require 'should'
describe 'Task Instance', ->
task1 = task2 = null
it 'should have …
Run Code Online (Sandbox Code Playgroud) 我正在使用QtScript自动化部分应用程序以进行开发和测试.我已经到了想要测试断言的地步,并基于"独立的断言库?" 我在Debian存储库中找到了什么,我选择了Should.js.
我无法将其加载到我的Qt应用程序中,因为它取决于Node的require()
功能.我尝试实现这个版本,从"支持CommonJS的require()"开始,最后得到下面的代码.
可以让它工作,还是我注定了这种方法?我可能最好将should.js的位复制到一个文件中吗?我宁愿不让自己负责保持叉子更新.(许可是一个非问题,因为我不打算重新分发此代码).
这是我的MCVE; 对不起,我不能再短了!
#include <QCoreApplication>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QScriptEngine>
#include <QScriptContext>
#include <QScriptContextInfo>
#include <QTextStream>
// Primitive implementation of Node.js require().
// N.B. Supports only .js sources.
QScriptValue require(QScriptContext* context, QScriptEngine* engine)
{
const QString moduleName = context->argument(0).toString();
// First, look in our modules cache
QScriptValue modules = engine->globalObject().property("$MODULES");
QScriptValue module = modules.property(moduleName);
if (module.isValid()) {
auto cached_file = module.property("filename");
auto time_stamp = …
Run Code Online (Sandbox Code Playgroud) 我是使用Mocha&should.js进行单元测试的新手.我正在使用Mocha的BDD来测试我的应用程序.我正在测试的应用程序有不同的组件,如帐户,产品和订单.在将代码移动到git存储库之前,我想测试应用程序的所有方面.我为所有组件提供了不同的测试文件.例如,帐户的account.js,订单的order.js等.
我想针对临时测试帐户测试所有组件.所以流程是:
我的问题是如何在执行其他测试之前确保创建临时帐户?
由于我在不同的文件中测试案例,我如何确保它们以与上述相同的顺序执行?还有其他更好的方法来测试应用程序吗?
谢谢.
should.js ×10
node.js ×7
mocha.js ×6
javascript ×3
unit-testing ×2
arrays ×1
bdd ×1
c++ ×1
coffeescript ×1
deepequals ×1
meteor ×1
promise ×1
qt ×1
qtscript ×1
sails.js ×1
tdd ×1