- 異步/等待規格,這在 2.0 中尚未提供,是比回呼式異步規格更好的選擇。將 Jasmine 1.x 異步規格直接轉換為異步/等待也更容易。
- 自訂比對器介面已在 4.0 中變更。有關變更內容的詳細資訊,請參閱4.0 移轉指南,或有關更多資訊,請參閱目前的自訂比對器文件。
- 未來主要版本的重大變更大部分與尚未存在於 1.x 中的 API 有關。您可以直接升級到最新版本來跳過這些變更。
本升級指南的其餘部分大致維持 2.0 版本發布時的狀態。
隨著 jasmine 2.0 的發布,許多事情已經改變 |
describe("Upgrading to jasmine 2.0", function() {
|
自訂比對器 |
describe("Custom Matchers", function() {
beforeEach(function() {
|
|
/* was:
this.addMatchers({
*/
jasmine.addMatchers({
|
比對器的設定方式現在略有不同。工廠收到一個 |
/* was:
toBeCustom: function(expected) {
var passed = this.actual == expected;
*/
toBeCustom: function(util, customEqualityTesters) {
return {
compare: function(actual, expected) {
var passed = actual == expected
|
比較現在應該傳回一個具有 有關自訂比對器使用方式的更多資訊。此頁面旨在顯示將 1.x 套件升級到 2.0 所需的變更 |
/* was:
this.message = function() {
return [
'Expected ' + this.actual + ' to equal ' + expected,
'Expected ' + this.actual + ' not to equal ' + expected
];
};
return passed;
});
*/
return {
pass: passed,
message: 'Expected ' + actual + (passed ? '' : ' not') + ' to equal ' + expected
};
}
};
}
});
});
|
自訂比對器的使用方式保持不變 |
it("uses custom matchers", function() {
expect(1).toBeCustom(1);
});
});
|
非同步規格 |
describe("Asynchronous Specs", function() {
|
我們會假設這對我們的以下測試而言是非同步的 |
var asyncSetThing,
somethingAsyncWithCallback = function(callback) {
asyncSetThing = true;
callback();
};
|
|
/* was:
it("calls an async thing and waits", function() {
var asyncDone = false;
somethingAsyncWithCallback(function() {
asyncDone = true
});
|
儘管過去有必要在規格本身中追蹤非同步狀態。 |
waitsFor(function() {
return asyncDone;
});
*/
|
透過具有 |
beforeEach(function(done) {
somethingAsyncWithCallback(done);
});
/*
runs(function() {
expect(asyncSetThing).toBeTruthy();
});
});
*/
it("will wait until async completes and calls done", function() {
expect(asyncSetThing).toBeTruthy();
});
});
|
間諜 |
describe("Spies", function() {
it('should spy', function() {
var spy = jasmine.createSpy('spy');
|
告訴間諜其行為方式的所有方法不再是對間諜上的其自身屬性。有一個單一的 |
/* was:
spy.andCallThrough();
spy.andCallFake(function() {});
spy.andThrow('error');
spy.andReturn(1);
*/
spy.and.callThrough();
spy.and.callFake(function() {});
spy.and.throwError('error');
spy.and.returnValue(1);
|
基本設定和檢查保持不變 |
spy('foo');
spy('bar');
expect(spy).toHaveBeenCalledWith('foo');
|
類似於行為,更進階的呼叫檢查位於 |
/* was:
expect(spy.mostRecentCall.args).toEqual(['bar']);
expect(spy.callCount).toBe(2);
*/
expect(spy.calls.mostRecent().args).toEqual(['bar']);
expect(spy.calls.count()).toBe(2);
});
});
|
時脈 |
describe("Clock", function() {
|
jasmine 偽時脈現在是一個實例化物件,而不是全域物件,而且現在會「安裝」它,而不是使用 |
beforeEach(function() {
/* was:
jasmine.Clock.useMock();
*/
jasmine.clock().install();
});
|
|
it("uses the clock similarly", function() {
/* was:
jasmine.Clock.tick();
*/
jasmine.clock().tick();
});
|
Jasmine 2.0 移除了附加程式動態新增 |
afterEach(function() {
jasmine.clock().uninstall();
});
});
});
|