1 /** 2 * Internal representation of a Jasmine specification, or test. 3 * 4 * @constructor 5 * @param {jasmine.Env} env 6 * @param {jasmine.Suite} suite 7 * @param {String} description 8 */ 9 jasmine.Spec = function(env, suite, description) { 10 if (!env) { 11 throw new Error('jasmine.Env() required'); 12 } 13 if (!suite) { 14 throw new Error('jasmine.Suite() required'); 15 } 16 var spec = this; 17 spec.id = env.nextSpecId ? env.nextSpecId() : null; 18 spec.env = env; 19 spec.suite = suite; 20 spec.description = description; 21 spec.queue = new jasmine.Queue(env); 22 23 spec.afterCallbacks = []; 24 spec.spies_ = []; 25 26 spec.results_ = new jasmine.NestedResults(); 27 spec.results_.description = description; 28 spec.matchersClass = null; 29 }; 30 31 jasmine.Spec.prototype.getFullName = function() { 32 return this.suite.getFullName() + ' ' + this.description + '.'; 33 }; 34 35 36 jasmine.Spec.prototype.results = function() { 37 return this.results_; 38 }; 39 40 jasmine.Spec.prototype.log = function(message) { 41 return this.results_.log(message); 42 }; 43 44 /** @deprecated */ 45 jasmine.Spec.prototype.getResults = function() { 46 return this.results_; 47 }; 48 49 jasmine.Spec.prototype.runs = function (func) { 50 var block = new jasmine.Block(this.env, func, this); 51 this.addToQueue(block); 52 return this; 53 }; 54 55 jasmine.Spec.prototype.addToQueue = function (block) { 56 if (this.queue.isRunning()) { 57 this.queue.insertNext(block); 58 } else { 59 this.queue.add(block); 60 } 61 }; 62 63 jasmine.Spec.prototype.addMatcherResult = function(result) { 64 this.results_.addResult(result); 65 }; 66 67 jasmine.Spec.prototype.expect = function(actual) { 68 return new (this.getMatchersClass_())(this.env, actual, this); 69 }; 70 71 jasmine.Spec.prototype.waits = function(timeout) { 72 var waitsFunc = new jasmine.WaitsBlock(this.env, timeout, this); 73 this.addToQueue(waitsFunc); 74 return this; 75 }; 76 77 jasmine.Spec.prototype.waitsFor = function(timeout, latchFunction, timeoutMessage) { 78 var waitsForFunc = new jasmine.WaitsForBlock(this.env, timeout, latchFunction, timeoutMessage, this); 79 this.addToQueue(waitsForFunc); 80 return this; 81 }; 82 83 jasmine.Spec.prototype.fail = function (e) { 84 var expectationResult = new jasmine.ExpectationResult({ 85 passed: false, 86 message: e ? jasmine.util.formatException(e) : 'Exception' 87 }); 88 this.results_.addResult(expectationResult); 89 }; 90 91 jasmine.Spec.prototype.getMatchersClass_ = function() { 92 return this.matchersClass || this.env.matchersClass; 93 }; 94 95 jasmine.Spec.prototype.addMatchers = function(matchersPrototype) { 96 var parent = this.getMatchersClass_(); 97 var newMatchersClass = function() { 98 parent.apply(this, arguments); 99 }; 100 jasmine.util.inherit(newMatchersClass, parent); 101 jasmine.Matchers.wrapInto_(matchersPrototype, newMatchersClass); 102 this.matchersClass = newMatchersClass; 103 }; 104 105 jasmine.Spec.prototype.finishCallback = function() { 106 this.env.reporter.reportSpecResults(this); 107 }; 108 109 jasmine.Spec.prototype.finish = function(onComplete) { 110 this.removeAllSpies(); 111 this.finishCallback(); 112 if (onComplete) { 113 onComplete(); 114 } 115 }; 116 117 jasmine.Spec.prototype.after = function(doAfter, test) { 118 119 if (this.queue.isRunning()) { 120 this.queue.add(new jasmine.Block(this.env, doAfter, this)); 121 } else { 122 this.afterCallbacks.unshift(doAfter); 123 } 124 }; 125 126 jasmine.Spec.prototype.execute = function(onComplete) { 127 var spec = this; 128 if (!spec.env.specFilter(spec)) { 129 spec.results_.skipped = true; 130 spec.finish(onComplete); 131 return; 132 } 133 this.env.reporter.log('>> Jasmine Running ' + this.suite.description + ' ' + this.description + '...'); 134 135 spec.env.currentSpec = spec; 136 137 spec.addBeforesAndAftersToQueue(); 138 139 spec.queue.start(function () { 140 spec.finish(onComplete); 141 }); 142 }; 143 144 jasmine.Spec.prototype.addBeforesAndAftersToQueue = function() { 145 var runner = this.env.currentRunner(); 146 for (var suite = this.suite; suite; suite = suite.parentSuite) { 147 for (var i = 0; i < suite.before_.length; i++) { 148 this.queue.addBefore(new jasmine.Block(this.env, suite.before_[i], this)); 149 } 150 } 151 for (var i = 0; i < runner.before_.length; i++) { 152 this.queue.addBefore(new jasmine.Block(this.env, runner.before_[i], this)); 153 } 154 for (i = 0; i < this.afterCallbacks.length; i++) { 155 this.queue.add(new jasmine.Block(this.env, this.afterCallbacks[i], this)); 156 } 157 for (suite = this.suite; suite; suite = suite.parentSuite) { 158 for (var i = 0; i < suite.after_.length; i++) { 159 this.queue.add(new jasmine.Block(this.env, suite.after_[i], this)); 160 } 161 } 162 for (var i = 0; i < runner.after_.length; i++) { 163 this.queue.add(new jasmine.Block(this.env, runner.after_[i], this)); 164 } 165 }; 166 167 jasmine.Spec.prototype.explodes = function() { 168 throw 'explodes function should not have been called'; 169 }; 170 171 jasmine.Spec.prototype.spyOn = function(obj, methodName, ignoreMethodDoesntExist) { 172 if (obj == jasmine.undefined) { 173 throw "spyOn could not find an object to spy upon for " + methodName + "()"; 174 } 175 176 if (!ignoreMethodDoesntExist && obj[methodName] === jasmine.undefined) { 177 throw methodName + '() method does not exist'; 178 } 179 180 if (!ignoreMethodDoesntExist && obj[methodName] && obj[methodName].isSpy) { 181 throw new Error(methodName + ' has already been spied upon'); 182 } 183 184 var spyObj = jasmine.createSpy(methodName); 185 186 this.spies_.push(spyObj); 187 spyObj.baseObj = obj; 188 spyObj.methodName = methodName; 189 spyObj.originalValue = obj[methodName]; 190 191 obj[methodName] = spyObj; 192 193 return spyObj; 194 }; 195 196 jasmine.Spec.prototype.removeAllSpies = function() { 197 for (var i = 0; i < this.spies_.length; i++) { 198 var spy = this.spies_[i]; 199 spy.baseObj[spy.methodName] = spy.originalValue; 200 } 201 this.spies_ = []; 202 }; 203 204