我有一些JavaScript,我正在測試Jasmine。在這個時候,我的套房之一是這樣的:在Jasmine中生成測試
var library = require('../library');
describe('My App -', function() {
afterEach(function(done) {
// some clean up code goes here
library.cleanUp(done);
done();
});
describe('Test Operation 1 -', function() {
beforeEach(function(done) {
library.init(done);
});
it('should load fine', function() {
if (library) {
expect(true).toBe(true);
} else {
expect(false).toBe(true);
}
});
var parameters = [1, 8.24, -1];
var results = [5, 4, 0];
// [TODO]: Create tests here
});
});
有我產生從我parameters
和results
陣列規格的方法嗎?換句話說,在運行時,我會實質類似的動態運行:
it('should be 5 when parameter is 1', function(done)) {
var result = library.calculate(1);
expect(result).toBe(5);
done();
});
it('should be 4 when parameter is 8.24', function(done) {
var result = library.calculate(8.24);
expect(result).toBe(4);
done();
});
it('should be 0 when parameter is -1', function(done) {
var result = library.calculate(-1);
expect(result).toBe(0);
done();
});
我不想如下:
it('should test the parameters', function() {
for (var i=0; i<parameters.length; i++) {
var result = library.calculate(parameters[i]);
expect(results[i]).toBe(result);
}
});
我試圖弄清楚如何動態地在運行時生成一些測試。
謝謝!
如果與循環您的問題是這些信息並不足夠具體,您可能需要查看茉莉花自定義消息(github上的庫)。它允許你有一個自定義/確切的消息,但在循環內。 – ktharsis
''''''''''''''''''可以放在'for'循環中,因此您將在它們中的每一箇中都有單個期望的多個規格,而不是一個具有多個期望的規格(就像它在最後的代碼示例中一樣)。 「it」的標題也可以動態創建,具體取決於當前的循環迭代。 –
@MichaelRadionov有什麼方法可以在'eval'中放一個'it'?如果是這樣,怎麼樣? –