Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery Ajax test does not work
    text
    copied!<p>I am trying to test a very simple jQuery plugin which simply calls $.ajax method and put its content into a element. For testing I am using JsTestDriver and Sinon for mocking.</p> <p>Plugin file looks:</p> <pre><code>(function($) { $.fn.transfer = function() { $.ajax({ url : 'friends', type : 'GET', contentType : 'application/json', dataType : 'json', success : function(html) { console.log("-"+html+"-"); console.log($(this)); $(this).html(html); console.log("+"+$(this).html()+"+") } }); }; })(jQuery); </code></pre> <p>In theory very simple plugin.</p> <p>And then I have written a unit test mocking success function:</p> <pre><code>TestCase("Ajax ", { 'test response' : function () { /*:DOC divElement = &lt;div id="container" style="height: 100px;"&gt;&lt;/div&gt;*/ sinon.stub(jQuery, "ajax").yieldsTo( "success", 'alex'); $(this.divElement).transfer(); console.log("*"+$(this.divElement).text()+"*"); } }); </code></pre> <p>it seems correct too. Then if you execute this test, next lines are printed through console:</p> <pre><code>[LOG] -alex- [LOG] [object Object] [LOG] +null+ [LOG] ** </code></pre> <p>So success function receives correctly "alex" string. Then $(this) reference is printed, message is set with html() function and when I log previous set value, a null is returned. And last log message is in test file, where as you can see ajax text is not set.</p> <p>Does anybody know what I am doing wrong? Because I am sure I am missing something that now I cannot see.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload