Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to write this JavaScript code without eval (QUnit mocking)?
    text
    copied!<p>I've been experimenting with QUnit tests and was looking for a good method to override functions with mocks so as to enable more atomic tests. There are good solutions out there for specific cases, for example overriding $.ajax (<a href="https://gist.github.com/1371254" rel="nofollow">Simple jQuery (1.5+) AJAX Mocking</a>), but I was looking for a more general approach and came up with this:</p> <pre><code>// Constructor for overrides. function overrides(overrides) { this.overrides = overrides; } // Implementation for overrides. overrides.prototype = { set: function () { var functions = {}; $.each(this.overrides, function (key, value) { eval("functions['" + key + "'] = " + key + ";"); eval(key + " = value;"); }); this.functions = functions; }, reset: function () { var functions = this.functions; $.each(this.overrides, function (key, _) { eval(key + " = functions['" + key + "'];"); }); } } </code></pre> <p>Which can then be used like:</p> <pre><code>module("Comments", { setup: function () { this.overrides = new overrides({ "$.ajax": function (url, options) { alert("ajax: " + url + ', ' + options); } }); this.overrides.set(); }, teardown: function () { this.overrides.reset(); } }); </code></pre> <p>Now, that all appears to work fine, and although this may not be the worst possible use of eval(), I was wondering if this could indeed be written without using eval()? I've read up on a bunch of the other eval() questions here and tried various options like accessing the overrides using window[] but that does not work for the $.ajax case for example (window['$'].ajax works but not window['$.ajax']).</p> <p>Perhaps I'm also thinking to hard and eval() can be used safely here or there is a better approach in general for function overrides?</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