前言
这是我翻译的Jasmine文档第三篇,前两篇文章的地址为:
匹配全局jasmine.any
jasmine.any
传递一个构造函数(constructor)或者“类”(class)作为一个期望值,如果实际构造函数和传递的参数相同则返回true。
例如:
describe("jasmine.any", function() {
it("matches any value", function() {
expect({}).toEqual(jasmine.any(Object));
expect(12).toEqual(jasmine.any(Number));
});
describe("when used with a spy", function() {
it("is useful for comparing arguments", function() {
var foo = jasmine.createSpy('foo');
foo(12, function() {
return true;
});
expect(foo).toHaveBeenCalledWith(jasmine.any(Number), jasmine.any(Function));
});
});
});
匹配存在jasmine.anything
jasmine.anything函数只要实际值不为null或undefined则返回true。
例如:
describe("jasmine.anything", function() {
it("matches anything", function() {
expect(1).toEqual(jasmine.anything());
});
describe("when used with a spy", function() {
it("is useful when the argument can be ignored", function() {
var foo = jasmine.createSpy('foo');
foo(12, function() {
return false;
});
expect(foo).toHaveBeenCalledWith(12, jasmine.anything());
});
});
});
局部匹配jasmine.objectContaining
jasmine.objectContaining()
用于匹配那些只关心实际值中是否存在预测键值对的时候,若存在则返回true。
例如:
describe("jasmine.objectContaining", function() {
var foo;
beforeEach(function() {
foo = {
a: 1,
b: 2,
bar: "baz"
};
});
it("matches objects with the expect key/value pairs", function() {
expect(foo).toEqual(jasmine.objectContaining({
bar: "baz"
}));
expect(foo).not.toEqual(jasmine.objectContaining({
c: 37
}));
});
describe("when used with a spy", function() {
it("is useful for comparing arguments", function() {
var callback = jasmine.createSpy('callback');
callback({
bar: "baz"
});
expect(callback).toHaveBeenCalledWith(jasmine.objectContaining({
bar: "baz"
}));
expect(callback).not.toHaveBeenCalledWith(jasmine.objectContaining({
c: 37
}));
});
});
});
局部数组匹配jasmine.arrayContaining
jasmine.arrayContaining()
用于匹配实际数组中是否存在预测的值,存在则返回true。
例如:
describe("jasmine.arrayContaining", function() {
var foo;
beforeEach(function() {
foo = [1, 2, 3, 4];
});
it("matches arrays with some of the values", function() {
expect(foo).toEqual(jasmine.arrayContaining([3, 1]));
expect(foo).not.toEqual(jasmine.arrayContaining([6]));
});
describe("when used with a spy", function() {
it("is useful when comparing arguments", function() {
var callback = jasmine.createSpy('callback');
callback([1, 2, 3, 4]);
expect(callback).toHaveBeenCalledWith(jasmine.arrayContaining([4, 2, 3]));
expect(callback).not.toHaveBeenCalledWith(jasmine.arrayContaining([5, 2]));
});
});
});
字符串匹配jasmine.stringMatching
jasmine.stringMatching()
用于你不想在一个较大的对象精确匹配字符串时,或者只是匹配spy预测值的部分字符串时,匹配成功返回true。
例如:
describe('jasmine.stringMatching', function() {
it("matches as a regexp", function() {
expect({foo: 'bar'}).toEqual({foo: jasmine.stringMatching(/^bar$/)});
expect({foo: 'foobarbaz'}).toEqual({foo: jasmine.stringMatching('bar')});
});
describe("when used with a spy", function() {
it("is useful for comparing arguments", function() {
var callback = jasmine.createSpy('callback');
callback('foobarbaz');
expect(callback).toHaveBeenCalledWith(jasmine.stringMatching('bar'));
expect(callback).not.toHaveBeenCalledWith(jasmine.stringMatching(/^bar$/));
});
});
});
定制不对称的测试器
当你需要检查符合一定标准但不必严格相等的一些东西时,通过调用asymmetricMatch
方法可以简单地制定一个自定义的不对称测试器。
例如:
describe("custom asymmetry", function() {
var tester = {
asymmetricMatch: function(actual) {
var secondValue = actual.split(',')[1];
return secondValue === 'bar';
}
};
it("dives in deep", function() {
expect("foo,bar,baz,quux").toEqual(tester);
});
describe("when used with a spy", function() {
it("is useful for comparing arguments", function() {
var callback = jasmine.createSpy('callback');
callback('foo,bar,baz');
expect(callback).toHaveBeenCalledWith(tester);
});
});
});
Jasmine Clock
该语法在Jasmine2.0修改。Jasmine Clock用于测试与时间有关的代码。
在需要操作时间的spec或者Suites中调用jasmine.clock().install()
来安装。而完成之后要确保卸载来回复原来的功能。
模拟JavaScript超时函数
你可以使用setTimeout或者setInterval超时同步执行已声明的函数。为了执行函数,使用jasmine.clock().tick()
函数来推动时间前进,该函数传递一个毫秒数作为参数。
模拟日期
Jasmine Clock同样可以用来模拟当前日期。如果不提供一个基准时间给mockDate()
,它将使用当前日期。
例如:
describe("Manually ticking the Jasmine Clock", function() {
var timerCallback;
beforeEach(function() {
timerCallback = jasmine.createSpy("timerCallback");
jasmine.clock().install();
});
afterEach(function() {
jasmine.clock().uninstall();
});
it("causes a timeout to be called synchronously", function() {
setTimeout(function() {
timerCallback();
}, 100);
expect(timerCallback).not.toHaveBeenCalled();
jasmine.clock().tick(101);
expect(timerCallback).toHaveBeenCalled();
});
it("causes an interval to be called synchronously", function() {
setInterval(function() {
timerCallback();
}, 100);
expect(timerCallback).not.toHaveBeenCalled();
jasmine.clock().tick(101);
expect(timerCallback.calls.count()).toEqual(1);
jasmine.clock().tick(50);
expect(timerCallback.calls.count()).toEqual(1);
jasmine.clock().tick(50);
expect(timerCallback.calls.count()).toEqual(2);
});
describe("Mocking the Date object", function(){
it("mocks the Date object and sets it to a given time", function() {
var baseTime = new Date(2013, 9, 23);
jasmine.clock().mockDate(baseTime);
jasmine.clock().tick(50);
expect(new Date().getTime()).toEqual(baseTime.getTime() + 50);
});
});
});
异步支持
该语法在Jasmine2.0开始支持。Jasmine同样支持执行包含异步操作。
调用beforeEach
,afterEach
,beforeAll
,afterAll
和it
时可以传递一个可选的单一变量,该变量在异步工作结束后可以调用。
Jasmine在判断超时失败前会默认等待5秒去完成异步操作。如果done
还没执行,而5秒时间到期,当前的spec会被标记为失败,而接下来的会继续执行,就好像那个done
已经调用了。
如果一个特定的spec需要更多时间,可以通过传递一个超时的值来调整。
如果所有的suite有一个不同的超时,jasmine.DEFAULT_TIMEOUT_INTERVAL
可以在全局设置,在任何给定的describe
之外。
describe("Asynchronous specs", function() {
var value;
beforeEach(function(done) {
setTimeout(function() {
value = 0;
done();
}, 1);
});
it("should support async execution of test preparation and expectations", function(done) {
//该片段在beforeEach里的done()调用之前不会开始执行
value++;
expect(value).toBeGreaterThan(0);
done();//该片段在该done()没执行前并不会完成测试
});
describe("long asynchronous specs", function() {
beforeEach(function(done) {
done();
}, 1000);
it("takes a long time", function(done) {
setTimeout(function() {
done();
}, 9000);
}, 10000);
afterEach(function(done) {
done();
}, 1000);
});
describe("A spec using done.fail", function() {
var foo = function(x, callBack1, callBack2) {
if (x) {
setTimeout(callBack1, 0);
} else {
setTimeout(callBack2, 0);
}
};
it("should not call the second callBack", function(done) {
foo(true,
done,
function() {
//done.fail()可以导致spec失败,而标志该spec为完成
done.fail("Second callback has been called");
}
);
});
});
});