Tag: jasmine

茉莉花 – 同样方法的两个间谍

我是Jasmine新手,我想知道我们是否可以使用相同的方法创建2个间谍。 这是我正在尝试的。 describe(‘something’, function () { beforeEach(function () { mySpy = jasmine.createSpyObj(‘mySpy’, ‘functionInInterest’); mySpy.functionInInterest.andCallFake(function (cb) {cb(something);}); } //Some Test Cases describe(‘Here is the action!’, function () { mySpy = jasmine.createSpyObj(‘mySpy’, ‘functionInInterest’); mySpy.functionInInterest.andCallFake(function (cb) {cb(somethingElse);}); //Some test cases that depends on somethingElse }); }); 之前的测试用例Here is the action! 取决于mySpy.functionInInterest.andCallFake(function (cb) {cb(something);}); 里面的测试用例Here is the action! 取决于mySpy.functionInInterest.andCallFake(function […]

为什么jQuery代码在我的jasmine测试中没有执行,或者至少没有执行?

我正在使用jasmine-rails gem运行Rails 3.2.8。 以下是我的Gemfile中描述茉莉花设置的行: jasmine (1.2.1) jasmine-core (>= 1.2.0) rack (~> 1.0) rspec (>= 1.3.1) selenium-webdriver (>= 0.1.3) jasmine-core (1.2.0) jasmine-headless-webkit (0.8.4) coffee-script jasmine-core (~> 1.1) multi_json rainbow sprockets (~> 2) jasmine-rails (0.1.0) jasmine jasmine-headless-webkit rails (>= 3.1.0) 我也在使用jasmine-jquery扩展。 这组函数提供了我在测试中使用的toHaveText方法。 我已将jasmine-jquery扩展文件保存到spec / javascripts / helpers目录中。 名为UserInvitationsSpec.js的测试文件直接位于spec / javascripts目录中,其中包含以下内容: describe (“my basic jasmine jquery test”, function(){ […]

使用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); } […]

茉莉花 – 触发事件不起作用

我正在尝试测试的代码: $(“.toggle_item”).on(“change”, function() { console.log(“change triggered”) item_name = $(this).data(“name”); value = $(this).prop(“checked”); if (Item.isValid(item_name) && cartModule.isUnique(item_name)) { cartModule.toggleItem(item_name, value); } }) 茉莉花规格: describe(“changing toggle item”, function() { beforeEach(function() { console.log(“in spec”) affix(“.toggle_item[data-name=’ladder’]”) spyOn(cartModule, “isUnique”) spyOn(Item, “isValid”) $(“.toggle_item”).trigger(“change”) }) it(“should check item for validity & cart for uniqueness”, function() { expect(Item.isValid).toHaveBeenCalledWith(“ladder”) expect(cartModule.isUnique).toHaveBeenCalledWith(“ladder”) }) }) 控制台日志输出表明触发器未触发。 日志输出: […]

如何在$(window).on(“load”,function(){})中测试代码; 在Jasmine

我在下面有一个javascript,它在页面加载时附加DIV并在3秒后隐藏它。 var testObj = { initialize: function() { var that = this; $(window).on(“load”, function() { (function ($) { //Append Div $(‘body’).append(“TEST”); })(jQuery); that.hideAppendedDiv(); }); }, hideAppendedDiv: function() { //Hide appended Div after 3s setTimeout(function(){ $(“div”).hide(); }, 3000); } }; //call Initialize method testObj.initialize(); 如何为代码中的方法编写Jasmine测试用例。

为什么我的茉莉花测试在这个指令上失败了?

我已经构建了一个角度指令onInputChange ,当用户通过单击输入外部(模糊)或按ENTER来更改输入值时,该指令应触发回调。 该指令可以像: 它使用以下代码: app.directive(‘onInputChange’, [ “$parse”, function ($parse) { return { restrict : “A”, require : “ngModel”, link : function ($scope, $element, $attrs) { // var dirName = “onInputChange”, callback = $parse($attrs[dirName]), evtNS = “.” + dirName, initial = undefined; // if (angular.isFunction(callback)) { $element .on(“focus” + evtNS, function () { initial = $(this).val(); […]

茉莉花unit testing元素不是:隐藏

我有一个AngularJS指令,我正在编写测试。 我已经在这个问题中重新创建了这个问题: http ://plnkr.co/edit/gHoGwI6TZhsgKVwIb1uI? p = preview 任何人都可以告诉我为什么第二次测试失败以及如何解决它? 码: var app = angular.module(“myApp”, []); app.directive(“myDirective”, function() { return { template: “Hello this is my directive!” } }); describe(“Testing hidden”, function() { var $compile, $scope, $document; beforeEach(inject(function(_$compile_, $rootScope, _$document_){ $compile = _$compile_; $scope = $rootScope.$new(); $document = _$document_; })); function getElement() { var element = angular.element(“”); […]

使用AngularJS中的Jasmine测试元素数量

我正在使用Jasmine for AngularJS编写端到端测试。 我正在使用Protractor来运行测试。 我有以下标记 我想测试一下,对于特定的页面实例,我有四个这样的图像。 这是我到目前为止所拥有的 describe(‘Phone detail view’, function() { beforeEach(function() { browser.get(‘app/index.html#/phones/nexus-s’); }); it(‘should display four thumbnails on the nexus-s page’, function() { expect(element(by.css(‘.phone-thumbs li img’)).length()).toBe(4); }); }); 问题是我得到一个错误说 TypeError: Object # has no method ‘length’ 我哪里错了?

无法获得$ .ajax.mostRecentCall与jasmine 2.0.2一起使用

让一个简单的例子工作真的很麻烦。 我使用的这个例子来自https://gist.github.com/Madhuka/7854709 describe(“Test for spies”, function() { function sendRequest(callbacks, configuration) { $.ajax({ url: configuration.url, dataType: “json”, success: function(data) { callbacks.checkForInformation(data); }, error: function(data) { callbacks.displayErrorMessage(); }, timeout: configuration.remainingCallTime }); } it(“should make an Ajax request to the correct URL”, function() { var configuration = { url : “http://www.google.com”, remainingCallTime : 30000 }; spyOn($, “ajax”); sendRequest(undefined, […]

Jasmine spyOn – 方法在jQuery匿名函数中不存在

在SO和其他论坛上搜索过这个问题的答案。 没有找到解决方案。 我遇到了一个问题,Jasmine无法在jQuery代码块中找到函数,无论Jasmine代码是否在jQuery函数内部。 $(function() { function foo(param1, param2) { // some code } describe(“Foo function”, function () { spyOn(window, ‘foo’); foo(1, 2); it(“Should be found by Jasmine”, function(){ expect(window.foo).toHaveBeenCalled(); }); }); }); Error: foo() method does not exist 我也尝试了spyOn($, ‘foo’) , spyOn($.fn, ‘foo’)和其他人。 此外,以下代码也不起作用。 $(function() { function foo(param1, param2) { // some code } }); […]