Tag: 茉莉花

在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 […]

在Jasmine测试期间插入(SpyOn 2次)jQuery值以输入

嗨我有工作测试 it(“should add a model”, function() { spyOn($.fn, “val”).and.returnValue(“Bar”); foodtype.addNewFoodtype(); //My view expect($(“#newFoodtype”).val()).toEqual(“Bar”); expect(foodtype.collection.length).toEqual(1); }); 我下次测试的时候 spyOn($.fn, “val”).and.returnValue(“Bar”); $(“#newFoodtype”).val() spyOn($.fn, “val”).and.returnValue(“Foo”); $(“#newFoodtype”).val() 检查更改但有错误 错误:spyOn:val已经被用于监视:spyOn(object,methodName)

如何使用特定的window.location.href在Jasmine中测试window.load()?

我有以下代码: window.onload= function() { var requestData = { bookId:$(‘.bookId’).text() }; if(window.location.href.indexOf(“/viewbook”)!=-1){ $.post(“check-book”, requestData, function (response) { if(response == “already saved”) { $(‘.add-btn’).attr(‘disabled’, ‘disabled’); } }); } }; 我正在尝试在Jasmine中编写一个合适的测试。 所以,我有以下代码: describe(“should disable button”, function () { it(“should check if book exists in list while loading page”, function () { var localContext = { “window”:{ location:{ href:”/viewbook” […]

无法在Jasmineunit testing中窥探ajax的交错成功回调

在源代码中,我有一个getJSON调用,在以下结构中有两个成功回调。 要测试的源代码: jsonMakeLineOrderData = $.getJSON(url, jsonData, function (data) { console.log(“Inside 1st callback”); //… some other statement }).success(function (data) { // line# 1 console.log(“Inside 2nd callback”); //… some other statement }); 我正在使用Jasmine测试框架来测试此调用的成功块。 假冒/模拟ajax调用我使用了spyOn实用程序。 我的Jasmine测试规范: it (“Test Function”,function(){ var data = ; var d; spyOn($, “ajax”).andCallFake(function(params) { params.success(data); // line# 2 d = $.Deferred(); d.resolve(data); return d.always(); […]

在jasmine上监视jQuery $(’…’)选择器

当涉及监视jQuery函数(例如bind , click等)时,很容易: spyOn($.fn, “bind”); 问题是当你想窥探$(‘…’)并返回已定义的元素数组时。 在SO上阅读其他相关答案后尝试了一些事情: spyOn($.fn, “init”).andReturn(elements); // works, but breaks stuff that uses jQuery selectors in afterEach(), etc spyOn($.fn, “merge”).andReturn(elements); // merge function doesn’t seem to exist in jQuery 1.9.1 spyOn($.fn, “val”).andReturn(elements); // function never gets called 那我该怎么做? 或者,如果唯一的方法是监视init函数,那么当我完成后如何从函数中“删除”间谍, afterEach()路由不会中断。 jQuery版本是1.9.1。 解决方法: 到目前为止我唯一可以使它工作的方式(丑陋): realDollar = $; try { $ = jasmine.createSpy(“dollar”).andReturn(elements); // […]

如何进行unit testing以查看角度是否未定义?

我正在使用角度版本1和茉莉花进行unit testing。 我想做的测试是: 当我在ie7中加载index.html页面时,我加载了一个html横幅,说下载最新的浏览器。 我目前正在索引页面上使用JQuery加载一个html模板。 是否可以测试这个,因为它在角度app之外。 这是我在index.html页面上的当前代码: if(window.angular === undefined) $(function(){$(“#angularNotSupportedModal”).load(“/app/noAngularModal.html”);}); 任何建议都会很棒。 谢谢