I'm using Karma/Jasmine to test a given class. I need to test that an array contains an object with a given property, i.e. I don't want to specify the whole object (it is rather large and the test would become less maintainable if I had to).
我正在使用Karma / Jasmine来测试给定的类。我需要测试一个数组包含一个具有给定属性的对象,即我不想指定整个对象(它相当大,如果必须的话,测试将变得不那么可维护)。
I've tried the following:
我尝试过以下方法:
expect(filters.available).toContain(jasmine.objectContaining({name:"majors"});
but this gave me the error 'jasmine' is not defined, and I haven't been able to figure out the cause of that error.
但这给了我错误'jasmine'没有定义,我无法弄清楚该错误的原因。
1 个解决方案
#1
11
One way of doing it in jasmine 2.0
is to use a custom matcher. I also used lodash
to iterate over the array and inthe objects inside each array item:
在jasmine 2.0中执行此操作的一种方法是使用自定义匹配器。我还使用lodash迭代数组和每个数组项中的对象:
'use strict';
var _ = require('lodash');
var customMatcher = {
toContain : function(util, customEqualityTesters) {
return {
compare : function(actual, expected){
if (expected === undefined) {
expected = '';
}
var result = {};
_.map(actual, function(item){
_.map(item, function(subItem, key){
result.pass = util.equals(subItem,
expected[key], customEqualityTesters);
});
});
if(result.pass){
result.message = 'Expected '+ actual + 'to contain '+ expected;
}
else{
result.message = 'Expected '+ actual + 'to contain '+ expected+' but it was not found';
}
return result;
}
};
}
};
describe('Contains object test', function(){
beforeEach(function(){
jasmine.addMatchers(customMatcher);
});
it('should contain object', function(){
var filters = {
available: [
{'name':'my Name','id':12,'type':'car owner'},
{'name':'my Name2','id':13,'type':'car owner2'},
{'name':'my Name4','id':14,'type':'car owner3'},
{'name':'my Name4','id':15,'type':'car owner5'}
]
};
expect(filters.available).toContain({name : 'my Name2'});
});
});
#1
11
One way of doing it in jasmine 2.0
is to use a custom matcher. I also used lodash
to iterate over the array and inthe objects inside each array item:
在jasmine 2.0中执行此操作的一种方法是使用自定义匹配器。我还使用lodash迭代数组和每个数组项中的对象:
'use strict';
var _ = require('lodash');
var customMatcher = {
toContain : function(util, customEqualityTesters) {
return {
compare : function(actual, expected){
if (expected === undefined) {
expected = '';
}
var result = {};
_.map(actual, function(item){
_.map(item, function(subItem, key){
result.pass = util.equals(subItem,
expected[key], customEqualityTesters);
});
});
if(result.pass){
result.message = 'Expected '+ actual + 'to contain '+ expected;
}
else{
result.message = 'Expected '+ actual + 'to contain '+ expected+' but it was not found';
}
return result;
}
};
}
};
describe('Contains object test', function(){
beforeEach(function(){
jasmine.addMatchers(customMatcher);
});
it('should contain object', function(){
var filters = {
available: [
{'name':'my Name','id':12,'type':'car owner'},
{'name':'my Name2','id':13,'type':'car owner2'},
{'name':'my Name4','id':14,'type':'car owner3'},
{'name':'my Name4','id':15,'type':'car owner5'}
]
};
expect(filters.available).toContain({name : 'my Name2'});
});
});