最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jasmine: check that an array contains an element with given properties - Stack Overflow

programmeradmin4浏览0评论

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 bee less maintainable if I had to).

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.

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 bee less maintainable if I had to).

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.

Share Improve this question asked Jun 21, 2014 at 12:46 dabsdabs 7471 gold badge8 silver badges24 bronze badges 1
  • 1 Where is this error ing from? Jshint/Jslint are both going to plain unless you declare jasmine as a global. Also, what version of jasmine are you using? I believe .objectContaining is 2.0+ only. – Nick Tomlin Commented Jun 21, 2014 at 16:18
Add a ment  | 

1 Answer 1

Reset to default 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:

'use strict';
var _ = require('lodash');
var customMatcher = {
    toContain : function(util, customEqualityTesters) {
        return {
            pare : 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'});
    });
});
发布评论

评论列表(0)

  1. 暂无评论