如果您仍在使用 Jasmine 1.x,您可能會發現直接跳到最新版本(截至撰寫本文為止為 5.x)比逐漸升級到各個主要版本更容易。特別是

本升級指南的其餘部分大致維持 2.0 版本發布時的狀態。

隨著 jasmine 2.0 的發布,許多事情已經改變

describe("Upgrading to jasmine 2.0", function() {

自訂比對器

  describe("Custom Matchers", function() {
    beforeEach(function() {

addMatchers 函式不再使用於規格(this),現在位於全域 jasmine 物件上。

      /* was:
         this.addMatchers({
      */
      jasmine.addMatchers({

比對器的設定方式現在略有不同。工廠收到一個 util 物件,其中包含事物的內容,例如 jasmine 的等同運算函式和任何已註冊的 customEqualityTesters。預期工廠會傳回一個具有 compare 函式的物件,該函式將直接呼叫 actualexpected,而不是 this 上的實際值

        /* was:
           toBeCustom: function(expected) {
             var passed = this.actual == expected;
        */
        toBeCustom: function(util, customEqualityTesters) {
          return {
            compare: function(actual, expected) {
              var passed = actual == expected

比較現在應該傳回一個具有 passmessage 屬性的物件。

有關自訂比對器使用方式的更多資訊。此頁面旨在顯示將 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();
    };

runswaitswaitsFor 方法已被移除,改為允許以規格的一部分來執行的函式接收和呼叫 done 回呼。

    /* was:
       it("calls an async thing and waits", function() {
         var asyncDone = false;
         somethingAsyncWithCallback(function() {
           asyncDone = true
         });

儘管過去有必要在規格本身中追蹤非同步狀態。

         waitsFor(function() {
           return asyncDone;
         });
     */

透過具有 beforeEachafterEachit 接收 done 回呼,jasmine 將等到該函式呼叫後才會移動到佇列中的下一項。這表示如果非同步邏輯也為其完成時採用回呼,jasmine 的 done 便可以傳遞,而 jasmine 將適當地等待。

    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');

告訴間諜其行為方式的所有方法不再是對間諜上的其自身屬性。有一個單一的 and 屬性,上面有所有的間諜行為,因此可以減少加到受間諜函式上的屬性。

       /* 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');

類似於行為,更進階的呼叫檢查位於 calls 屬性上

      /* 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 偽時脈現在是一個實例化物件,而不是全域物件,而且現在會「安裝」它,而不是使用 useMock

    beforeEach(function() {
    /* was:
       jasmine.Clock.useMock();
     */
      jasmine.clock().install();
    });

tick 時脈的基本方式保持不變

    it("uses the clock similarly", function() {
      /* was:
         jasmine.Clock.tick();
       */
      jasmine.clock().tick();
    });

Jasmine 2.0 移除了附加程式動態新增 afterEach 回呼的能力。為了防止時脈成為一個能夠解除安裝自己的特殊物件,現在需要手動解除其安裝。

    afterEach(function() {
      jasmine.clock().uninstall();
    });
  });
});