Tag: sinon

将jQuery加载到React App的Mocha测试中

我已经创建了一个类似于本教程中概述的Mocha测试设置: https : //github.com/jesstelford/react-testing-mocha-jsdom 。 我想知道如何将jQuery加载到这个结构中。 我把它作为require(‘jquery’)测试JS文件包含在内,但是当我调用$.ajax ,它会给出一个错误,说明TypeError: Attempted to wrap undefined property ajax as function 。 我怀疑这是因为Node没有在浏览器中运行我的JavaScript。 但是,我尝试过使用窗口环境 ,但仍然没有运气。 我知道传统的方法是在HTML标签中包含jQuery,但我不知道如何在这里做,因为我没有HTML文件。

如何使用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 […]

Sinon的假服务器没有响应

还有一些其他问题询问Sinon没有回应,但他们似乎都解决了一些平凡的问题,如无效的响应数据或切换配置选项。 我的情况如下: 在主应用程序(at /js/app/ )中,requireJS用于加载网站应用程序模块。 对于测试(at /js/test ),requireJS也用于加载相同的模块,但添加了Mocha , Chai和Sinon 。 这是test应用程序的引导程序: define( “testRunner”, [“require”, “chai”, “module”, “sinon”, “mocha”], function( require, chai, module ){ // Chai setup assert = chai.assert; should = chai.should(); expect = chai.expect; // Mocha setup mocha.setup( ‘bdd’ ); // tests require( module.config().tests, function(){ mocha.run(); } ); } ); require([“testRunner”]); module.config().tests在requirejs.config({})调用中定义为: “config”: […]

如何使用Sinon存根AJAX调用

我有一个函数,它向端点发出一个AJAX请求并获取JSON,我如何使用Sinon伪造该AJAX请求,以便我可以测试该函数是否正常工作?

使用Sinon.js并阻止对我的应用服务器的调用

问题很简单: 我希望我们sinon.js测试一段javascript以确保它在做两件事时调用$.ajax方法: 我不想真正打到服务器 我想模拟服务器的响应 所以这是JS: $.ajax url: “/tickets/id.json” dataType: ‘json’ .done (data) => HandlebarsTemplates[“tickets/popup_title”] data 这是我的测试: describe ‘PopupDisplayer’, -> beforeEach -> loadFixtures ‘popup_displayer’ new PopupDisplayer @title_stub = sinon.stub( HandlebarsTemplates, “tickets/popup_title”) @jquery_stub = sinon.stub(jQuery, ‘ajax’).yieldsTo(‘done’, {}) //This triggers the ajax call $(‘.popupable .title’).mouseenter() afterEach -> HandlebarsTemplates[‘tickets/popup_title’].restore() HandlebarsTemplates[‘tickets/popup_content’].restore() jQuery.ajax.restore() @server.restore() it ‘renders the title with the data […]

使用Sinon在D3中测试Mouseover事件

我在试图通过测试时遇到了麻烦。 我希望能够使用间谍来检查鼠标hover事件是否被正确调用。 目前我收到以下错误,“错误:预计至少被调用一次,但从未被调用过”。 我的部分困惑与d3和jQuery选择器之间的差异有关,我非常乐意使用后者,但我不确定如何在测试中正确使用前者来获得我想要的结果。 我的依赖是d3,jQuery,mocha,chai,sinon和sinon-chai。 我的index.html文件中的相关代码, mocha.ui(‘bdd’); mocha.reporter(‘html’); var expect = chai.expect; mocha.run(); fixtures.js, var path = svg.selectAll(“path”) .data(pie(data)) .enter().append(“path”).attr(“class”, “path”) .style(“fill”, function(d, i) { return color(i); }) .attr(“d”, arc) .on(“mouseover”, function() { d3.select(this).style(“fill”, “#ff0000”); }) .on(“mouseout” , function() { d3.selectAll(“path”).style(“fill”, function(d, i) { return color(i); }); }); // I wanted to test my understanding […]

Sinon FakeServer没有请求?

我正在关注SinonJS 假服务器教程 ,我正在运行这个简单的代码: var server; before(function () { server = sinon.fakeServer.create(); }); after(function () { server.restore(); }); it(“calls callback with deserialized data”, function () { var callback = sinon.spy(); getTodos(42, callback); // This is part of the FakeXMLHttpRequest API server.requests[0].respond( 200, { “Content-Type”: “application/json” }, JSON.stringify([{ id: 1, text: “Provide examples”, done: true }]) ); […]

使用jasmine.js和sinon.js调用backbone.js click事件间谍

我正在尝试使用backbone.js,jasmine.js和sinon.js测试按钮单击。 但是以下测试用例失败了。 我正在使用间谍来追踪它是否被召唤。 你能帮我解决这个问题吗? 谢谢。 新任务模板 Add Task NewTaskView T.views.NewTaskView = Backbone.View.extend({ tagName: ‘section’, id: ‘new_task_section’, template : _.template ( $(“#new_task_template”).html() ), initialize: function(){ _.bindAll( this, ‘render’, ‘addTask’); }, events:{ “click #add_new_task” : “addTask” }, render: function(){ $(this.el).html( this.template() ); return this; }, addTask: function(event){ console.log(“addTask”); } }); 茉莉花测试案例 describe(“NewTaskView”, function(){ beforeEach( function(){ this.view = […]

存根jQuery选择器调用?

我正在努力改进unit testing我的JavaScript。 我有以下代码: var categoryVal = $(‘#category’).val(); if (categoryVal === ”) { doSomething(); } 我的测试#category器在页面上没有#category输入,那么我如何在这里存根/模拟jQuery选择器呢? 我已经查看了jasmin和sinon文档,但无法弄清楚如何让它们在这里工作,因为它们的存根操作对象,而$不是。