茉莉花部分字符串匹配

iwo*_*uff 10 javascript jasmine karma-jasmine

我喜欢 jasmine.objectContaining 提供的部分对象匹配:

mySpy({
   foo: 'bar',
   bar: 'baz' 
});
expect(mySpy).toHaveBeenCalledWith(jasmine.objectContaining({ foo: 'bar' }));
Run Code Online (Sandbox Code Playgroud)

有茉莉花相当于字符串吗?大致如下:

mySpy('fooBar', 'barBaz');
expect(mySpy).toHaveBeenCalledWith(jasmine.stringContaining('foo'), jasmine.any(String));
Run Code Online (Sandbox Code Playgroud)

我想看看一个特定的论点,而不诉诸 mySpy.calls 的断言:

mySpy('fooBar', 'barBaz');
expect(mySpy.calls.argsFor(0)[0]).toContain('foo');
Run Code Online (Sandbox Code Playgroud)

Riv*_*ver 12

从 Jasmine 2.2 开始,您可以使用jasmine.stringMatching

对于你的例子:

mySpy('fooBar', 'barBaz');
expect(mySpy).toHaveBeenCalledWith(jasmine.stringMatching('foo'), jasmine.any(String));
Run Code Online (Sandbox Code Playgroud)

请注意,参数是要匹配的正则表达式。对于简单的“包含”,直接传递字符串(使用特殊的正则表达式字符转义),但它可以做更多的事情:

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


Sid*_*era 3

茉莉花里没有这样的东西。但您可以利用在 Jasmine 中创建自定义匹配器的功能来实现此目的。

这是一个小工作示例:

您的工厂

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);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

使用自定义匹配器的测试用例

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');
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!