Tag: unit testing

在Jasmine嘲笑xhr

我有一个关于在Jasmine中嘲笑xhr的问题。 我有以下Javascript情况: function Test1(){ // some code Test2({ readyState: 4, responseText: “”, status: 200, statusText: “OK” }); } function Test2(xhr){ var token = xhr.getResponseHeader(“csrftoken”); var csrfCtrl = $(“#token”); if (token != null && csrfCtrl != null) { csrfCtrl.val(token); } } 现在我想窥探xhr.getResponseHeader()函数,但我无法知道如何做到这一点。 我试过这样的事情: describe(“1 || Test ||”, function () { // Before we describe the tests […]

如何对jQuery Promises进行unit testing?

我有以下四个函数,当使用参数进行promise解析时,将调用two(),three()和four()。 让我再解释一下。 当我调用函数one()时,我传递默认参数值,但函数two()将在函数one()中使用promise的解析值调用。 函数two(),three()和four()遵循类似的逻辑。 function one(arg) { var deferred = $.Deferred(); // Don’t worry yet what this is until after you understand the flow console.log(“Starting one’s ajax with arg: ” + arg); $.ajax({ url: ‘/’, success: function() { // Here’s where you want to call the next function in the // list if there is […]

WebStorm – Jasmine jQuery

我对WebStorm的使用还很陌生,但它没有像预期的那样工作。 我过去有一个项目,但现在我尝试重新创建它并尝试对我的代码进行unit testing。 我对unit testingJavaScript代码非常陌生。 所以,我有一个文件karma.conf.js ,其中包含以下内容: module.exports = function(config){ config.set({ basePath : ‘./’, files : [ ‘../../Resources/External/Bower/angular/angular.js’, ‘../../Resources/External/Bower/angular-mocks/angular-mocks.js’, ‘../../Resources/Scripts/OfficeUI.js’, ‘../../Unit Tests/**/*.Test.js’ ], autoWatch : true, frameworks: [‘jasmine-jquery’, ‘jasmine’], browsers : [‘Chrome’] }); }; 所以,我使用Jasmine框架与Karma一起运行我的测试。 我已经包含了jasmine-jquery ,以便在我的HTML代码上加载fixture。 所以,我有一个JavaScript函数,可以在按下或释放按钮时切换类: $(function() { $(‘#OfficeUI a.button’).on(‘mousedown mouseup’, function(e) { $(this).toggleClass(‘ie-fix’); }); }); 现在,我编写了一个unit testing,如下所示: describe(“Unit: OfficeUI Elements”, function() { […]

Clojurescript / Reagent Unit测试组件 – 模拟onChange

对于我有文本框的组件,我需要能够从测试中更改其中的文本: (defn choose-city-component [] (let [inner-state (r/atom {:text “”})] (fn [] [:div [:input#txt_city { :type “text” :value (@inner-state :text) :on-change #(swap! inner-state assoc :text (-> % .-target .-value))… 在测试中,我在屏幕上渲染它: (deftest choose-city-component-test-out ;;GIVEN render component in test (let [comp (r/render-component [w/choose-city-component] (. js/document (getElementById “test”)))] ;;WHEN changing the city…. 现在使用jQuery触发器我试图在文本上模拟onChange: 我们尝试了 (.change ($ :#txt_city) {“target” {“value” […]

使用Jasmine模拟jQuery ajax调用

我正在使用Jasmine 2.5.2为使用jQuery 3.1.1执行Ajax请求的代码编写unit testing。 我想模拟Ajax调用,提供我自己的响应状态和文本。 我正在使用Jasmine ajax插件( https://github.com/pivotal/jasmine-ajax )。 按照https://jasmine.github.io/2.0/ajax.html上的示例,它使用XMLHttpRequest对象,工作正常。 describe(“mocking ajax”, function() { describe(“suite wide usage”, function() { beforeEach(function() { jasmine.Ajax.install(); }); afterEach(function() { jasmine.Ajax.uninstall(); }); it(“specifying response when you need it”, function() { var doneFn = jasmine.createSpy(“success”); var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(args) { if (this.readyState == this.DONE) { doneFn(this.responseText); } […]

make javascript function callback-ish

我正在使用jquery的javascript函数: my.namespace.loadSomeStuff = function() { $.get(‘/Services/loadStuff’, function(c){ $(“#stuffDiv”).html(c); }); }; 我正在尝试为此编写unit testing。 现在,因为这个函数并没有真正返回任何东西并使用jquery的’get’异步加载一些东西我很难测试它。 基本上我想做的是调用这个函数,等到它返回一些东西,然后访问加载的元素来validation…类似于: my.namespace.loadSomeStuff(function(){ alert(‘boo’); assertSame(‘Login’, $(“#stuffDiv”).find(……); }); 现在,我在调用unit testing的模板中创建了一个,它将正确加载和显示,但警报或断言永远不会执行。 我有没有办法在不增加大量额外代码的情况下实现这一目标? 谢谢

如何使用Sinon.js将附加到’this’对象的方法存根?

我正在尝试测试下面附带的’onInit’函数: function (jQuery, Controller, JSONModel) { “use strict”; var controls; var mainController = Controller.extend(“tool.controller.Main”, { onInit: function(oEvent) { var inputModel = new JSONModel(“./model/inputs.json”); var productsModel = new JSONModel(“./model/products.json”); this.getView().setModel(inputModel, “inputModel”); this.getView().setModel(productsModel); controls = viewControls.main.apply(this); }, … 目的 特别是对于这个函数,我只需要测试调用’setModel’和’main’方法。 我面临的问题 为了将这个’onInit’函数正确地进行unit testing,我需要使用stub / mock进行一些依赖。 具体来说,这些依赖关系将如下: ‘this’关键字/对象,附加了’getView’方法 ‘JSONModel’对象构造函数 我坚持理解如何模仿上述内容。 以下是我初学者对Sinon.js的理解: QUnit.test(“‘onInit’ Function Test”, function(assert) { // Arrange […]

使用Zombie.jsvalidationQunit的结果

我正在使用Qunit进行测试驱动开发:在创建新函数时,我为它编写测试,创建函数,重新加载页面,如果所有测试都通过,我继续…虽然这在开始时工作正常,一段时间后,它开始成为一个耗时的过程,因为所有测试都需要几秒钟才能运行,而这是我每次刷新浏览器时都要等待的时间。 为了解决这个问题,我考虑引入Zombie.js来执行无头测试:想法是让Zombie.js不断检查网页(例如$ watch -n1 “node queryTheWebpage.js” )并报告给编码时Qunits的结果(偶尔,由于Zombie.js不是“真正的”浏览器,我会打开一个浏览器并手动检查页面进行validation)。 到目前为止,这里是我的节点/ Zombie代码片段: browser.visit(“http://localhost/mywebpage.html”, function () { var qunit_tests = browser.query(‘body’); console.log(qunit_tests.innerHTML); }); 在控制台输出中,我确实看到Qunit测试容器 但它是空的,这意味着当调用visit回调函数时,测试没有运行。 我试过使用wait函数等待测试运行,但是没有成功: function waitForQunitToEnd(window) { var last = window.document.querySelector(‘selectorOfMyLastTest’); var first_failed = window.document.querySelector(‘li.failed’); return (last || first_failed); } browser.visit(“http://localhost/mywebpage.html”, function () { browser.wait(waitForQunitToEnd, function() { var qunit_tests = browser.query(‘body’); console.log(qunit_tests.innerHTML); // still gives me empty […]

如何检测QUnit模块中的所有测试何时完成?

出于测试排序的目的以及防止测试被中断。 此外,有没有办法停止模块或中途测试并重置QUnit(包括所有历史记录结果)? QUnit.moduleDone是我尝试测试模块完成的唯一方法。 但它适用于模块中的每个单独测试,而不仅仅是模块整体。

如何在github上的存储库中的子文件夹中搜索特定的单词或条目

我只是在寻找一个示例(在github的高级搜索中触发的正确命令)关于在特定存储库的文件夹中搜索单词。 我想这将涉及“路径:”选项,但不知道如何使用它 作为一个例子,我需要在以下位置的jquery测试套件中搜索函数名称“clean”或“clean”或类似函数的测试用例 – https://github.com/jquery/jquery/tree/master/test 我知道有一些filter,如repo:和path:由github提供,但不知道如何使用它们。 此外,我对在类似问题上发布的答案不满意 – 在github存储库中搜索定义给定函数的文件 我知道我可以将它分叉到我的本地机器并在那里进行搜索,但我不想下载整个存储库只是为了搜索它。 我只需要在github网站上在线搜索它。 提前致谢 !!