-1

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
...
Community
  • 1
  • 1
Jessi
  • 701
  • 1
  • 11
  • 22
  • Possible duplicate of [How to return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – Louis Oct 08 '15 at 10:36

1 Answers1

0

In that post towards the end of that most epic answer, the author mentions to simply not use $.getJSON.

I think you misunderstood that part of the answer. You cannot use $.getJSON if you want the Ajax request to be synchronous. However, you should not event want the request to be synchronous. I mean, the section is titled with "Not recommended: Synchronous "AJAX" calls", I mean not recommend when I say it :)


You should be using callbacks or promises to handle the response, as it is explained earlier in the answer. There it says you should be returning the return value of $.getJSON (a promise / deferred object):

Codelib.prototype.gotjson = function() {
  return $.getJSON("https://api.twitch.tv/kraken/streams/MedryBW");
};

and have the calling code register the callback:

codelib.gotjson().done(function(result) {
  assert.equal(result, 1, 'should be true');
});

However, QUnit expects tests to be synchronous, so it doesn't wait until the Ajax response was received. Luckily, QUnit supports async tests as well:

var done = assert.async();
codelib.gotjson().done(function(result) {
  assert.equal(result, 1, 'should be true');
  done();
});
Felix Kling
  • 705,106
  • 160
  • 1,004
  • 1,072