jasmine

Using Jasmine to spy on a function without an object

China☆狼群 提交于 2019-12-27 17:07:49
问题 I'm new to Jasmine and have just started using it. I have a library js file with lots of functions which are not associated with any object (i.e. are global). How do I go about spying on these functions? I tried using window/document as the object, but the spy did not work even though the function was called. I also tried wrapping it in a fake object as follows : var fakeElement = {}; fakeElement.fakeMethod = myFunctionName; spyOn(fakeElement, "fakeMethod"); and test with expect(fakeElement

How do I verify jQuery AJAX events with Jasmine?

我只是一个虾纸丫 提交于 2019-12-27 10:57:53
问题 I am trying to use Jasmine to write some BDD specs for basic jQuery AJAX requests. I am currently using Jasmine in standalone mode (i.e. through SpecRunner.html ). I have configured SpecRunner to load jquery and other .js files. Any ideas why the following doesn't work? has_returned does not become true, even thought the "yuppi!" alert shows up fine. describe("A jQuery ajax request should be able to fetch...", function() { it("an XML file from the filesystem", function() { $.ajax_get_xml

How to deal with thrown errors in async code with Jasmine?

本小妞迷上赌 提交于 2019-12-25 14:25:10
问题 The following test causes Jasmine (2.3.4, run in browser via Karma) to crash and not run any subsequent tests it('should report as failure and continue testing', function (done) { setTimeout(function () { throw new SyntaxError('some error'); done(); }, 1000); }); How can I have this test correctly report itself as a failure and carry on with subsequent tests? 回答1: Mocking the clock will give you the expected result. Mocking the clock in general is a best practice for testing timeouts.

Template-Url Directive Unit Testing without Karma

谁说我不能喝 提交于 2019-12-25 11:54:40
问题 SOLUTION = Solution Plunker I have tried manually passing the template in testing - is it even good way of doing it ? How to make it passes !!!!!!! How to write unit test for Simple directive with template-Url without Using Karma . I have tried following after seeing examples on stack-overflow but no success. Directive app.directive("crazy", function () { return { restrict: "A", templateUrl:"directivetemplate.html" }; }); Spec describe('Directive: crazy', function () { beforeEach(module(

How to test json inside function using jasmine?

↘锁芯ラ 提交于 2019-12-25 10:26:09
问题 This is the code that I want to test: function loadNotification(searchOption, searchKey) { var url = '@URLs.API.Notifications.Past' + '?searchOption=' + searchOption + '&searchValue=' + searchKey; $.getJSON(url) .done(function (nData) { //some code here }) .fail(function (jqXHR, status, error) { showError('There was an error while fetching the records. Please try after some time. (' + jqXHR.status + ':' + jqXHR.statusText + ')'); }); } Using jasmine , how do I test if the json file fails or

Unit test of $cacheFactory removeAll() in service failing in Angular 1.2.0 but working in 1.0.7

荒凉一梦 提交于 2019-12-25 09:31:35
问题 I have a unit test for an Angular service in which I test that a cache $cacheFactory is cleared after a call has been made for a save() method that does an http post to the backend. In 1.0.7 this test passed in Karma and Jasmine Specrunner.html, but after migrating to Angular 1.2.0 it fails. I have not changed any code in the service or in the spec file. The cache is cleared in production when I check it manually. Any ideas? EDIT: Plunk of the error in action: http://plnkr.co/edit/1INhdM The

Testing code within addEventHandler

社会主义新天地 提交于 2019-12-25 09:10:05
问题 I have the following - window.addEventListener( "orientationchange", () => { // Code to test }); How can I trigger this event in Jasmine? I have tried the following and it didn't work - event = document.createEvent("HTMLEvents"); event.initEvent("orientationchange", true, true); Here is the class - export class AvailabilityComponent { changed: boolean = false; constructor( ) { window.addEventListener( "orientationchange", () => { this.changed = !this.changed; } ); } } Thanks 回答1: You can use

Jasmine Spec as Typescript File

99封情书 提交于 2019-12-25 09:09:48
问题 I'm attempting to set up unit testing in my project, using Jasmine. I am writing my specs in Typescript. My first test is simply checking that a config file returns a value as expected. However, when I import the config, Jasmine can't find the spec. If I take out the import and fill in dummy values, everything works fine. My spec file is: /// <reference path="../typings/index.d.ts"/> process.env.ENV = "test"; process.env.TEST_DB_NAME= "test"; import environment = require("../config/config");

Testing code within addEventHandler

天涯浪子 提交于 2019-12-25 09:08:16
问题 I have the following - window.addEventListener( "orientationchange", () => { // Code to test }); How can I trigger this event in Jasmine? I have tried the following and it didn't work - event = document.createEvent("HTMLEvents"); event.initEvent("orientationchange", true, true); Here is the class - export class AvailabilityComponent { changed: boolean = false; constructor( ) { window.addEventListener( "orientationchange", () => { this.changed = !this.changed; } ); } } Thanks 回答1: You can use

How to mock calls to JQuery without including JQuery in my spec file at all

北慕城南 提交于 2019-12-25 09:05:55
问题 The function I want to test is mostly pure JavaScript (CoffeeScript), but includes one line that calls Jquery: $("input##{name}").change -> I don't want to include Jquery in my Jasmine files at all. I'd like to mock it out completely so this is a pure unit test. My spec for this function includes the following: class $ change: () -> return spy = spyOn($, 'change') However, Jasmine is giving me this error: Error: change() method does not exist How can I mock out $('input##{name}').change ->