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

javascript - jasmine partial string matching - Stack Overflow

programmeradmin1浏览0评论

I love the partial object matching that jasmine.objectContaining provides:

mySpy({
   foo: 'bar',
   bar: 'baz' 
});
expect(mySpy).toHaveBeenCalledWith(jasmine.objectContaining({ foo: 'bar' }));

Is there a jasmine equivalent for strings? something along the lines of:

mySpy('fooBar', 'barBaz');
expect(mySpy).toHaveBeenCalledWith(jasmine.stringContaining('foo'), jasmine.any(String));

I'd like to look at a specific argument without resorting to assertions off mySpy.calls:

mySpy('fooBar', 'barBaz');
expect(mySpy.calls.argsFor(0)[0]).toContain('foo');

I love the partial object matching that jasmine.objectContaining provides:

mySpy({
   foo: 'bar',
   bar: 'baz' 
});
expect(mySpy).toHaveBeenCalledWith(jasmine.objectContaining({ foo: 'bar' }));

Is there a jasmine equivalent for strings? something along the lines of:

mySpy('fooBar', 'barBaz');
expect(mySpy).toHaveBeenCalledWith(jasmine.stringContaining('foo'), jasmine.any(String));

I'd like to look at a specific argument without resorting to assertions off mySpy.calls:

mySpy('fooBar', 'barBaz');
expect(mySpy.calls.argsFor(0)[0]).toContain('foo');
Share Improve this question edited Oct 11, 2016 at 19:09 robbmj 16.5k8 gold badges39 silver badges63 bronze badges asked Oct 11, 2016 at 16:44 iwoodruffiwoodruff 1771 gold badge3 silver badges11 bronze badges 1
  • Possible duplicate of Is it possible to use Jasmine's toHaveBeenCalledWith matcher with a regular expression? – jannis Commented Jul 10, 2017 at 13:29
Add a comment  | 

3 Answers 3

Reset to default 13

As of Jasmine 2.2, you can use jasmine.stringMatching.

For your example:

mySpy('fooBar', 'barBaz');
expect(mySpy).toHaveBeenCalledWith(jasmine.stringMatching('foo'), jasmine.any(String));

Note that the argument is a regex to be matched. For simple "contains", pass the string directly (with special regex chars escaped), but it can do much more:

// Expect first parameter to be a string *starting* with "foo"
expect(mySpy).toHaveBeenCalledWith(jasmine.stringMatching(/^foo/), jasmine.any(String));

There isn't anything of this sort in Jasmine. But you can leverage the ability of creating a custom matcher in Jasmine for this.

Here's a small working example:

Your Factories

angular.module('CustomMatchers', []).factory('AnotherService', function(){
    return{  mySpy: function(a, b){ } }
});

angular.module('CustomMatchers').factory('MyService', function(AnotherService){
    return{ 
        myFunction: function(a, b){
            AnotherService.mySpy(a, b);
        }
    }
});

Your test case with a custom matcher

describe('Service: MyService', function() {
    beforeEach(module('CustomMatchers'));
    describe('service: MyService', function() {

        beforeEach(inject(function(_MyService_, _AnotherService_) {
            MyService = _MyService_;
            AnotherService = _AnotherService_;

            spyOn(AnotherService, 'mySpy');

            jasmine.addMatchers({
                toContain: function() {
                    return {
                        compare: function(actual, expected){
                            var result = { pass: actual.includes(expected) };
                            if(result.pass) {
                                result.message =  "Success: Expected first arguments '" + actual + "' to contain '"+ expected +"'.";
                            } else {
                                result.message =  "Failour: Expected first arguments '" + actual + "' to contain '"+ expected +"'.";
                            }
                            return result;
                        }
                    }
                }
            });

        }));

        it('expect AnotherService.mySpy toHaveBeenCalled', function() {
            MyService.myFunction('fooBar', 'barBaz');
            //This should pass
            expect(AnotherService.mySpy.calls.argsFor(0)[0]).toContain('foo');

            //This should fail
            expect(AnotherService.mySpy.calls.argsFor(0)[0]).toContain('helloWorld');
        });
    });
});

Hope this helps!

You can do like this with toString() and match() functions

    expect(control.errors.pattern.requiredPattern).toString().match('^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\S\./0-9]*$');
发布评论

评论列表(0)

  1. 暂无评论