2015-09-16 123 views
4

我正在嘗試編寫一個Jasmine的插件,它允許您從規範中返回一個承諾,並根據承諾是否被滿足或拒絕而通過或失敗該規範。測試茉莉花測試是否失敗

當然,我想編寫測試以確保我的插件能夠正常工作,並且要徹底,我需要確保在承諾被拒絕時測試失敗......所以如何通過測試當我需要確保測試「成功失敗」時?

回答

5

與誰茉莉花工作開發人員的談話後,我們來到了這一點:

var FAILED = 'failed' 
var PASSED = 'passed' 

describe('My Test Suite', function() { 
    var env 

    beforeEach(function() { 
     // Create a secondary Jasmine environment to run your sub-specs in 
     env = new jasmine.Env() 
    }) 

    it('should work synchronously', function() { 
     var spec 

     // use the methods on `env` rather than the global ones for sub-specs 
     // (describe, it, expect, beforeEach, etc) 
     env.describe('faux suite', function() { 
      spec = env.it('faux test', function (done) { 
       env.expect(true).toBe(true) 
      }) 
     }) 

     // this will fire off the specs in the secondary environment 
     env.execute() 

     // put your expectations here if the sub-spec is synchronous 
     // `spec.result` has the status information we need 
     expect(spec.result.status).toBe(FAILED) 
    }) 

    // don't forget the `done` argument for asynchronous specs 
    it('should work asynchronously', function (done) { 
     var spec 

     // use the methods on `env` rather than the global ones. 
     env.describe('faux suite', function() { 
      // `it` returns a spec object that we can use later 
      spec = env.it('faux test', function (done) { 
       Promise.reject("FAIL").then(done) 
      }) 
     }) 

     // this allows us to run code after we know the spec has finished 
     env.addReporter({jasmineDone: function() { 
      // put your expectations in here if the sub-spec is asynchronous 
      // `spec.result` has the status information we need 
      expect(spec.result.status).toBe(FAILED) 
      // this is how Jasmine knows you've completed something asynchronous 
      // you need to add it as an argument to the main `it` call above 
      done() 
     }}) 

     // this will fire off the specs in the secondary environment 
     env.execute() 
    }) 
}) 
2

變成爲關喬的回答,讓我感動的假測試情境成一個單一的功能。由於測試中的代碼正在利用茉莉花的期望,因此我將內部Env加載到jasmine.currentEnv_中,並用jasmine.currentEnv_.expect()明確地調用它。請注意,currentEnv_是由茉莉花本身設置的內部變量,所以我不能保證這在未來的茉莉花版本中不會被破壞。

function internalTest(testFunc) { 
    var outerEnvironment = jasmine.currentEnv_; 
    var env = new jasmine.Env(); 
    jasmine.currentEnv_ = env; 
    var spec; 
    env.describe("fake suite", function() { 
     spec = env.it("fake test", function() { 
      func(); 
     }); 
    }); 

    env.execute(); 

    jasmine.currentEnv_ = outerEnvironment; 

    return spec.result; 
} 

然後每個測試看起來像

it("does something", function() { 
    //Arrange 

    //Act 
    var result = internalTest(function() { 
     //Perform action 
    }); 

    //Assert 
    expect(result.status).toBe("failed"); //Or "success" 
    expect(result.failedExpectations.length).toBe(1); 
    expect(result.failedExpectations[0].message).toBe("My expected error message"); 
}); 
+0

尼斯思考。這是一個很好的小抽象。但是,不要使用'jasmine.currentEnv_',也許應該將'env'作爲參數傳遞給'func'。另外,你如何處理異步規格的情況? –