我想做一个简单的测试,看看我是否得到了投掷的运作。所以我为自己做了一个例子,但它崩溃了,我不知道为什么。
asyncTest('Testing submission system', function(){
... // Decaration of the mockCreators
throws(function(){ // Text book example
throw 'error'
}, 'throws an error')
mainView.registerSave(mockCreator);
mainView.registerSave(mockCreator2);
mainView.registerSave(mockCreator3);
mainView.registerSave(mockCreator4);
throws(mainView.registerSave(mockCreator2), 'also throws an error')
mainView.startParsing();
});
我的实际mainView.registerSave
:
registerSave: function(object)
{
if(typeof(object.model) === 'undefined')throw 'Crash: Missing "model" attribute'
if(typeof(object.parseData) !== 'function')throw 'Crash: Missing "parseData" function'
if(typeof(object.saveData) !=='function')throw 'Crash: Missing "saveData" function'
if(_.indexOf(this.subscribed, object) !== -1)throw 'Save already registered'
this.subscribed.push(object);
},
mainView
已从Backbone.views
崩溃:
Died on test #2 at QUnit.asyncTest (http://localhost:8080/JSLib/qunit-1.14.0.js:108:9)
at HTMLDocument.<anonymous> (http://localhost:8080/CreateStory/testCreateStory.js:292:2)
at fire (http://localhost:8080/JSLib/jquery-1.11.1.js:3119:30)
at Object.self.fireWith [as resolveWith] (http://localhost:8080/JSLib/jquery-1.11.1.js:3231:7)
at Function.jQuery.extend.ready (http://localhost:8080/JSLib/jquery-1.11.1.js:3443:13)
at HTMLDocument.completed (http://localhost:8080/JSLib/jquery-1.11.1.js:3474:10): Save already registered
还有更多我使用这些图书馆:
答案 0 :(得分:0)
throws的第一个参数必须是回调,而不是方法调用本身,试试这个:
throws(function() { mainView.registerSave(mockCreator2);}, 'also throws an error')