qunit

Testing for a function that contains asynchronous code

倖福魔咒の 提交于 2019-12-11 08:16:32
问题 With qUnit, I understand how to use asyncTest() if you have asynchronous code within your tests, but what if you have a function that contains asynchronous code? In other words, the asynchronous request is not within the test, but is simply part of the code that is being tested. Take this code for example: function makeAjaxCall(){ $.get('/mypage', {}, function(data){ // Do something with `data` },'json'); } How can I call makeAjaxCall() within a test and then run tests on the data that is

Trouble running Qunit tests with static server using grunt

梦想的初衷 提交于 2019-12-11 05:51:36
问题 Running the tests via a web browser works fine, but using grunt gives me errors. I struggle to understand what I'm doing wrong here. grunt tests fails with $ grunt tests Running "jsonlint:sample" (jsonlint) task >> 4 files lint free. Running "jshint:all" (jshint) task >> 4 files lint free. Running "connect:server" (connect) task Started connect web server on http://localhost:5000 Running "qunit:all" (qunit) task Testing http://localhost:5000/tests/tests.html F >> Settings.json tests -

While unit testing I frequently require to test internal (private) logic, what is the best practice for this?

霸气de小男生 提交于 2019-12-11 05:33:05
问题 I believe everyone encounters a case when there's necessity to test internal wiring of the class/object. I know that in compiled languages this could be done through conditional compiling. Is this what I should do for JavaScript as well? What's the usual way to accomplish such task? Maybe I should just treat the class/object as a Black Box and only test it's outcomes? 回答1: Testing object's public contract (ie. black box testing you mentioned) most of the times should be enough. Proper test

How to unit test javascript function that calls getJSON

爱⌒轻易说出口 提交于 2019-12-11 03:39:08
问题 I've been struggling with unit test for 2 days now and there is something I can't achieve regarding async test. I'm new to unit test and I don't understand why this doesn't work. I have a file login.js that calls a $.getJSON(url, data, function) and returns a string with the status of login ("success" or "fail"). The call to $.getJSON uses mockjax to get the data (it wraps an ajax call). The login function works ok, when called from a test webpage using jQuery click event. But now I'm trying

Calling trigger() doesn't always fire when called from QUnit

橙三吉。 提交于 2019-12-11 02:38:16
问题 Although I've used QUnit in the past, it's been a year since I really used it, so I am hoping this is something trivial! I have a bunch of QUnit tests that are working really well, apart from one that fails every other time that the page is loaded. If I refresh, the test works, but if I refresh again it breaks! It's become predictable but I cannot see why it's not working. The test in question should use a trigger('change') event, but this only fires every other time. I have a dynamically

Visual Studio's Test Runner with Chutzpah won't recognize QUnit tests when using both Requirejs and knockoutjs

孤人 提交于 2019-12-11 01:59:29
问题 I'm trying to get VS 2013 test explorer with Chutzpah to recognize QUnit tests while I'm using require.js in combination with knockoutjs I've found some good resources listed below but I think I must just have one missing piece. This is what I used to ensure I was using Qunit and require.js together correctly. From this resource, it sounds like I need a Chutzpah.json file as well. Here is what I can reproduce: If I just use Chutzpah and qunit I can get it to work so I know I have Chutzpah

Qunit qunit-fixture div gets deleted running unit tests with Resharper 8

懵懂的女人 提交于 2019-12-10 17:45:58
问题 I have an issue that when I try to append or set up HTML code in the div id="qunit-fixture" when testing with Qunit running under ReSharper 8. The div id="qunit-fixture" is deleted for some reason. I need to test events specified in the document ready function but can not do so if I can not add the elements in the div id="qunit-fixture". Are there any solutions for this? 回答1: It does not get deleted. It is simply not there with the Resharper QUnit test runner! You can see for yourself when

Obtaining $httpBackend in QUnit tests

吃可爱长大的小学妹 提交于 2019-12-10 16:47:00
问题 I am writing QUnit tests for an Angular controller. In the setup function of module, I have written the following statements to get an object of $httpBackend : var injector = angular.injector(['ng']); var httpBackend = injector.get('$httpBackend'); In a test, a mock response for GET is configured as follows: httpBackend.expectGET(url).respond([]); The test spec fails at this statement with error: Object doesn't support property or method expectGET I am able to get other objects like

Best way to test Javascript functions that use variables from a closure?

末鹿安然 提交于 2019-12-10 15:49:48
问题 So the problem I'm having is I have a function which uses a variable in a closure and when testing it it returns a reference to the variable in it's scope. My code looks similar to the following: var app = function() { var theString = ""; //Appends ztvars onto the url passed into the function var appendString = function(url) { if (theString.length === 0) { return url; } return url+theString; }; //Used for testing, returns the function appendPageVars this.returnFunctions = function() { return

Test for visible in QUnit test of JQueryUI widget

时光毁灭记忆、已成空白 提交于 2019-12-10 13:52:55
问题 This may be obvious to everyone else but I didn't find it by searching, so posting both the question and one possible answer here. Background: Custom JQuery UI widget using widget factory In the widget, some elements get hidden or shown based on other data/options. Creating unit tests to verify they are being shown/hidden correctly. I thought that my unit tests could each create their own element in memory, similar to this old answer. Stripped out the actual widget part from this example: