我是Mocha和Chai的新手。我创建了一个用于比较测试中两个对象的函数。
function compareExtremelyCompexObject (testedObject, trueObject);
如何编写使用我的compareExtremelyCompexObject
函数来主张测试的摩卡柴式规格?
我有这样的东西:
it('should create a specific complex object from boilerplate data', function(done) {
importDataFromSystem().
.end(function(err, res){
var dummyComplexObject = getBoilerplateComplexObject();
compareExtremelyCompexObject(res, dummyComplexObject);
done();
});
});
});
我迄今为止发现的示例缺少如何比较复杂对象。可以用"应该"/"期望"来实现吗?
让我知道这是否还不够清晰。我真的已经研究了这个问题了几天。任何帮助将不胜感激!
我认为您应该稍微编辑一些问题,以简化,但是根据我的收集,您想用自定义功能主张新对象===测试对象吗?如果是这种情况,并且假设compareExtremelyCompexObject
返回对或错,那么您几乎在那里。
it('should create a specific complex object from boilerplate data', function(done) {
importDataFromSystem()
.end(function(err, res){
var dummyComplexObject = getBoilerplateComplexObject();
// with assert
assert(compareExtremelyCompexObject(res, dummyComplexObject));
// or with chai expect
expect(compareExtremelyCompexObject(res, dummyComplexObject)).to.be.true;
done();
});
});
});
根据您的评论,importDataFromSystem
链接的方式意味着它返回流,承诺或本身。假设它是一个流在" end"上的回调的流,然后res
可能是您要寻找的,因此上面的示例应该起作用。但是,如果res
不是您要寻找的,那么您可能必须解决诺言并链接到thebales,以确保同步操作顺序。例如
it('should create a specific complex object from boilerplate data', function(done) {
Promise.resolve()
.then(function(){
return importDataFromSystem();
})
.then(function(){
return assert(compareExtremelyCompexObject(getNewlyCreatedObject(), getBoilerplateComplexObject()));
// assert should throw error and be caught if the two objects are not equal
})
.then(function(){
done()
})
.catch(function(err){
done( err );
});
});
但是,当然,您需要某种方法来获取您创建的对象。这是其他讨论。您应该编辑您的问题,以缩小主题,以便仅处理自定义比较的主张。(或冒险降低投票,我将被代理人投票。=])