I keep getting "undefined" when trying to test for the presence of my json object. I do not understand why?
REVISION: I have read...in depth...the post listed here...How do I return the response from an asynchronous call?
In that post towards the end of that most epic answer, the author mentions to simply not use $.getJSON. I don't see that as an option in my case. My situation is different in that I need to use $.getJSON in order to, well, get json. Also, my configuration is different in that my $.getJSON call is inside a prototype method inside an AMD module. That article did help me understand that I can return the whole $.getJSON back and I have updated my code to reflect that. So now...
What do I need to do so that when I call codelib.gotjson from my test file and test the value of something inside the resultant Object?
NOTE: I can see inside an "Object" in my chrome console that console.dir(result) lets me see. And inside that object I can see a "responseText" containing the precious precious json string I am after. But I am now stuck on how to write an assert for it?
I want to write something like ....
assert.equal(Object.responseText.name,"bob", "is equal to bob")
I am soooo close now. Any help is appreciated. Thank you.
codelib.js
"use strict";
define(function() {
//constructor
function Codelib(a,b){
// if u had passed vars
this.b = b;
this.a = a;
}
//methods
Codelib.prototype.code = function(a, b) {
return (a + b);
};
//methods
Codelib.prototype.gotjson = function() {
return $.getJSON("https://api.twitch.tv/kraken/streams/MedryBW")
.done(function (data) {
console.log('gotJSON: ');
console.dir(data);
})
.fail(function (jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
console.log("Request Failed: " + err);
});
};
return Codelib;
});
test file codeTest.js
"use strict";
define(['src/codelib','jquery'], function(Codelib){
var run = function(){
QUnit.test('code should return the sum of the two supplied numbers.',function(assert){
var codelib = new Codelib();
assert.equal(codelib.code(1,1),2, 'The return should be 2.');
assert.equal(codelib.code(-2,1),-1, 'The return should be -1.');
});
QUnit.test("As a user, I can see whether MedryBW is currently streaming on Twitch.tv",function(assert){
var codelib = new Codelib();
var result = codelib.gotjson();
console.log('in test: ');
console.dir(result);
assert.equal(codelib.gotjson(),1, 'should be true');
});
};
return {run: run}
});
NOTE: Resultant Object found in chrome console:
Object:
...
responseText: "{"_links": {"self":"https://api.twitch.tv/kraken/streams/medrybw"...etc
...