目前,我有一个函数,有时会返回一个内部有一些函数的对象。使用expect(...).toEqual({...})
时,它似乎与这些复杂对象不匹配。具有函数或File
类的对象(来自输入类型文件),它不能。如何克服这个?
答案 0 :(得分:18)
expect(_.isEqual(obj1, obj2)).toEqual(true);
如果可行,您可以创建custom matcher:
this.addMatchers({
toDeepEqual: function(expected) {
return _.isEqual(this.actual, expected);
});
});
所以你可以这样编写规范:
expect(some_obj).toDeepEqual(expected_obj);
答案 1 :(得分:13)
正如Vlad Magdalin在评论中指出的那样,将对象变为JSON字符串,它可以像它一样深,并且函数和File / FileList类。当然,在函数上代替toString()
,它可以被称为'Function'
function replacer(k, v) {
if (typeof v === 'function') {
v = v.toString();
} else if (window['File'] && v instanceof File) {
v = '[File]';
} else if (window['FileList'] && v instanceof FileList) {
v = '[FileList]';
}
return v;
}
beforeEach(function(){
this.addMatchers({
toBeJsonEqual: function(expected){
var one = JSON.stringify(this.actual, replacer).replace(/(\\t|\\n)/g,''),
two = JSON.stringify(expected, replacer).replace(/(\\t|\\n)/g,'');
return one === two;
}
});
});
expect(obj).toBeJsonEqual(obj2);
答案 2 :(得分:5)
如果有人像我一样使用node.js,那么当我只关注比较简单属性而忽略所有函数时,我在Jasmine测试中使用以下方法。此方法需要json-stable-stringify,用于在序列化之前对对象属性进行排序。
<强>用法:强>
var stringify = require('json-stable-stringify');
var obj1 = {
func: function() {
},
str1: 'str1 value',
str2: 'str2 value',
nest1: {
nest2: {
val1:'value 1',
val2:'value 2',
someOtherFunc: function() {
}
}
}
};
var obj2 = {
str2: 'str2 value',
str1: 'str1 value',
func: function() {
},
nest1: {
nest2: {
otherFunc: function() {
},
val2:'value 2',
val1:'value 1'
}
}
};
it('should compare object properties', function () {
expect(stringify(obj1)).toEqual(stringify(obj2));
});
答案 3 :(得分:4)
扩展@Vlad Magdalin的答案,这在Jasmine 2中有效:
http://jasmine.github.io/2.0/custom_matcher.html
beforeEach(function() {
jasmine.addMatchers({
toDeepEqual: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
var result = {};
result.pass = _.isEqual(actual, expected);
return result;
}
}
}
});
});
如果您正在使用Karma,请将其放入启动回调中:
callback: function() {
// Add custom Jasmine matchers.
beforeEach(function() {
jasmine.addMatchers({
toDeepEqual: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
var result = {};
result.pass = _.isEqual(actual, expected);
return result;
}
}
}
});
});
window.__karma__.start();
});
答案 4 :(得分:2)
这是我使用Jasmine 2
语法的方式。
我在../support/customMatchers.js
中创建了一个customMatchers模块(我喜欢制作模块)。
"use strict";
/**
* Custom Jasmine matchers to make unit testing easier.
*/
module.exports = {
// compare two functions.
toBeTheSameFunctionAs: function(util, customEqualityTesters) {
let preProcess = function(func) {
return JSON.stringify(func.toString()).replace(/(\\t|\\n)/g,'');
};
return {
compare: function(actual, expected) {
return {
pass: (preProcess(actual) === preProcess(expected)),
message: 'The functions were not the same'
};
}
};
}
}
然后在我的测试中使用以下内容:
"use strict";
let someExternalFunction = require('../../lib/someExternalFunction');
let thingBeingTested = require('../../lib/thingBeingTested');
let customMatchers = require('../support/customMatchers');
describe('myTests', function() {
beforeEach(function() {
jasmine.addMatchers(customMatchers);
let app = {
use: function() {}
};
spyOn(app, 'use');
thingBeingTested(app);
});
it('calls app.use with the correct function', function() {
expect(app.use.calls.count()).toBe(1);
expect(app.use.calls.argsFor(0)).toBeTheSameFunctionAs(someExternalFunction);
});
});