jasmine定义了4个名字空间,每个名字空间下定义了不同的方法或者变量。使用相应名字空间下的方法可以配置jasmine或者断言测试。
- jasmine
- matchers
- async-matchers
- Spy#calls
1、Namespace: jasmine
这里只列举来一些常用的方法或变量,详情请参看:Namespace: jasmine
addCustomEqualityTester(tester)
:在当前测试作用域中添加一个自定义的equality tester。这个函数只能在beforeEach,it或beforeAll中调用。自定义equality请参看:custom equalityaddMatchers(matchers)
:在当前测试作用域中添加一个自定义的matcher,这个函数只能在beforeEach,it或beforeAll中调用。自定义matcher请参看:custom matcherany(clazz)
:clazz: 构造器或者类,若实际值是某个构造器或类的实例则成功。anything()
:若当前值是不是null和undefined则成功arrayContaining(sample)
:当实际值是一个数组并且至少包含sample中的元素则成功。arrayWithExactContents(sample)
:当前值是一个数组并且只包含sample中所有元素(不管顺序)则成功clock()
:返回当前Jasmine上下文中模拟的Clock。createSpy(name, originalFn)
:创建一个spy,该spy不会有任何的实现,使用originalFn来模拟真实的实现。createSpyObj(baseName, methodNames)
:创建一个spy对象,methodNames可以是Array或者对象,若是数组则根据数组元素创建spy,若是对象,它的key将成为方法名,值成为returnValue。empty()
:若实际值是空则成功。falsy()
:若实际值是null,undefined,0,false或者任何假值则成功。notEmpty()
:若实际值是非空则成功。objectContaining(sample)
:sample:Object,若实际值至少包含sample中的key/value则成功。stringMatching(expected)
:expected:RegExp | String,若实际值是一个字符串,且匹配RegExp或者String则成功。truthy()
:若实际值可以转化为true则成功。
2、Namespace: matchers
not
:反转expect,expect(something).not.toBe(true);
nothing()
:期望nothingexpect().nothing();
toBe(expected)
:期望实际值和预期值一致,使用===操作expect(thing).toBe(realThing);
toBeCloseTo(expected, precision)
:期望实际值在预期值的指定精度内,precision:小数点位数。expect(number).toBeCloseTo(42.2, 3);
toBeDefined()
:期望实际值是已被定义的 (不是undefined),例如:expect(result).toBeDefined();
toBeFalse()
:期望实际值是falseexpect(result).toBeFalse();
toBeFalsy()
:期望实际值是一个假值expect(result).toBeFalsy();
toBeGreaterThan(expected)
:期望实际值大于预期值expect(result).toBeGreaterThan(3);
toBeGreaterThanOrEqual(expected)
:期望实际值大于等于预期值expect(result).toBeGreaterThanOrEqual(25);
toBeLessThan(expected)
:期望实际值小于预期值expect(result).toBeLessThan(0);
toBeLessThanOrEqual(expected)
:期望实际值小于等于预期值expect(result).toBeLessThanOrEqual(123);
toBeNaN()
:期望实际值是一个NaN(不是一个Number)expect(thing).toBeNaN();
toBeNegativeInfinity()
:期望实际值是-Infinityexpect(thing).toBeNegativeInfinity();
toBeNull()
:期望实际值是nullexpect(result).toBeNull();
toBePositiveInfinity()
:期望实际值是Infinityexpect(thing).toBePositiveInfinity();
toBeTrue()
:期望实际值是trueexpect(result).toBeTrue();
toBeTruthy()
:期望实际是一个真值expect(thing).toBeTruthy();
toBeUndefined()
:期望实际值是undefinedexpect(result).toBeUndefined():
toContain(expected)
: 期望实际值包含预期值expect(array).toContain(anElement); expect(string).toContain(substring);
toEqual(expected)
:期望实际值等于预期值,使用深遍历比较expect(bigObject).toEqual({"foo": ['bar', 'baz']});
toHaveBeenCalled()
;期望实际值(例如spy)已经被调用expect(mySpy).toHaveBeenCalled(); expect(mySpy).not.toHaveBeenCalled();
toHaveBeenCalledBefore(expected)
:期望实际值(spy)在期望值(另一个spy)之前已经被调用expect(mySpy).toHaveBeenCalledBefore(otherSpy);
toHaveBeenCalledTimes(expected)
:期望实际值已经被调用期望值的指定的次数expect(mySpy).toHaveBeenCalledTimes(3);
toHaveBeenCalledWith()
:期望实际值(spy)至少用指定的参数调用一次expect(mySpy).toHaveBeenCalledWith('foo', 'bar', 2);
toHaveClass(expected)
:期望实际值是一个dom元素,并且具有相应的classvar el = document.createElement('div');el.className = 'foo bar baz'; expect(el).toHaveClass('bar');
toMatch(expected)
:期望实际值匹配正则表达式expect("my string").toMatch(/string$/); expect("other string").toMatch("her");
toThrow(expected)
:expected: 可选的,期望函数throw somethingexpect(function() { return 'things'; }).toThrow('foo');expect(function() { return 'stuff'; }).toThrow();
toThrowError(expected, message)
:期望一个函数throw一个Errorexpect(function() { return 'things'; }).toThrowError(MyCustomError, 'message'); expect(function() { return 'things'; }).toThrowError(MyCustomError, /bar/); expect(function() { return 'stuff'; }).toThrowError(MyCustomError); expect(function() { return 'other'; }).toThrowError(/foo/); expect(function() { return 'other'; }).toThrowError();
toThrowMatching(predicate)
:期望函数throw something匹配指定的值,throw的值将作为predicate的参数传入expect(function() { throw new Error('nope'); }).toThrowMatching(function(thrown) { return thrown.message === 'nope'; });
3、Namespace: async-matchers
toBeRejected()
:期望一个promise rejected,await expectAsync(aPromise).toBeRejected(); return expectAsync(aPromise).toBeRejected();
toBeRejectedWith(expected)
:期望一个promise rejected 一个值,并该值与预期值相等,使用深度比较。await expectAsync(aPromise).toBeRejectedWith({prop: 'value'}); return expectAsync(aPromise).toBeRejectedWith({prop: 'value'});
toBeRejectedWithError(expected, message)
:期待一个promise rejected 一个值,并且该值是期待值实例,若expected没有设置,则使用Error
。await expectAsync(aPromise).toBeRejectedWithError(MyCustomError, 'Error message'); await expectAsync(aPromise).toBeRejectedWithError(MyCustomError, /Error message/); await expectAsync(aPromise).toBeRejectedWithError(MyCustomError); await expectAsync(aPromise).toBeRejectedWithError('Error message'); return expectAsync(aPromise).toBeRejectedWithError(/Error message/);
toBeResolved()
:期待一个promise resolvedawait expectAsync(aPromise).toBeResolved(); return expectAsync(aPromise).toBeResolved();
toBeResolvedTo(expected)
:期望一个promise resolved 一个值,并该值与预期值相等,使用深度比较。await expectAsync(aPromise).toBeResolvedTo({prop: 'value'}); return expectAsync(aPromise).toBeResolvedTo({prop: 'value'});
4、Namespace: calls
all()
:该spy所有原始的调用数组。allArgs()
:以数组的方式顺序返回该spy所有调用的参数any()
:检查该spy是否被调用过argsFor(index)
:返回spy指定调用中的某个参数。count()
:返回spy调用的次数。first
:获取spy的第一次调用。mostRecent()
:获取spy的最近一次调用。reset()
:重置该spy,以使它变为没有调用过的状态。saveArgumentsByValue
:保存该spy调用参数的一个浅克隆,然后传递给每个调用。