jasmine

Jasmine: test a setTimeout function throws an error

こ雲淡風輕ζ 提交于 2019-12-10 17:26:50
问题 I would like to test the error handling of a method, which schedules work with setTimeout. The error will be thrown in the scheduled part, i.e.: function sutWithSetTimeout() { setTimeout(function () { throw new Error("pang"); }, 1); } How do I test that an error is thrown and that it has the correct message? 回答1: You need to catch the function in the setTimeout and call them using expect(function(){fn();}).toThrow(e); . So you can spy on setTimeout to get the function: spyOn(window,

How to test my jQuery plugin with Gulp, Jasmine and NodeJS?

半城伤御伤魂 提交于 2019-12-10 17:16:34
问题 I created a simple jQuery plugin, which modifies the HTML according to some simple rules, using jQuery. Now I need to test that it works. I use Gulp for build automation. I decided to use Jasmine for unit testing. My question is how do I run my plugin from the test.js and validate the result? I have node.js installed at the build server. 回答1: Solution Use jsdom to emulate a browser within your Jasmine unit tests. Details jsdom is "a pure-JavaScript implementation of many web standards...for

Declaring getter property when building SpyObj using jasmine.createSpyObj utility?

本小妞迷上赌 提交于 2019-12-10 17:09:44
问题 Let's say I have a class: class MyRealClass { get propOne() { return stuffFromTheServer; } } When testing, I want to achieve this functionality: const mockClass = { get propOne() { return someStuff; } } jasmine.spyOnProperty(mockClass, 'propOne', 'get'); By doing something like this... const spy = jasmine.createSpyObj('mockClass', [ {methodName: 'propOne', accessType: 'get'} ]); In other words, I want to build a SpyObj<MyRealClass> using the jasmine.createSpyObj and declare the getter

How to create stub for ajax function using Jasmine BDD

与世无争的帅哥 提交于 2019-12-10 16:51:34
问题 I'm struggling to find any examples on how to fake an ajax call using Jasmine BDD? I have a custom ajax function that works like so... ajax({ url: 'JSON.php', dataType: 'json', onSuccess: function(resp) { console.log(resp); } }); ...and I've no idea how to create a stub to fake calling the actual ajax function. I want to avoid calling the ajax function as it could slow down my test suite if a real ajax call to the server takes some time to respond and I've loads of specs in my test suite. I

Testing requireJS methods async with Jasmine

南笙酒味 提交于 2019-12-10 16:47:57
问题 I am trying to test a function that requires a module using jasmine and requirejs . Here is a dummy code: define("testModule", function() { return 123; }); var test = function() { require(['testModule'], function(testModule) { return testModule + 1; }); } describe("Async requirejs test", function() { it("should works", function() { expect(test()).toBe(124); }); }); It fails, because it is an async method. How can I perform a test with it? Note: I dont want to change my code, just my tests

Does Jasmine's spyOn() allow the spied on function to be executed?

与世无争的帅哥 提交于 2019-12-10 16:31:17
问题 Does Jasmine's spyOn() method allow the spied on function to be executed, or does it, kind of - intercept the invocation when the spied method is (about to get) invoked, and returns true . PS: Could anyone point me to the explanation of spyOn() 's inner workings? 回答1: Spy : A spy can pretend to be a function or an object that you can use while writing unit test code to examine behavior of functions/objects var Person = function() {}; Dictionary.prototype.FirstName = function() { return "My

Test for the number of elements using Jasmine in AngularJS

家住魔仙堡 提交于 2019-12-10 16:16:50
问题 I am writing an end to end test using Jasmine for AngularJS. I am using Protractor to run the test. I have the following markup <ul class="phone-thumbs"> <li ng-repeat="img in phone.images"> <img ng-src="{{img}}"> </li> </ul> And I want to test that for a particular page instance I have four of these images. Here's what I have so far describe('Phone detail view', function() { beforeEach(function() { browser.get('app/index.html#/phones/nexus-s'); }); it('should display four thumbnails on the

Jasmine unable to find Spec files

不问归期 提交于 2019-12-10 15:34:06
问题 In my MEAN project directory, I am trying to run Jasmine unit tests on my express controller. I was initially running node-jasmine however for added functionality I am changing to Jasmine 2.4. However I am running into problems with the location of the jasmine.json, and when I try and workaround, I run into errors in the jasmine source code. My jasmine.json currently reads: { "spec_dir": "spec", "spec_files": "**/*[sS]pec.js" } My file structure is: http://imgur.com/WHg0u8M my jasmine.json is

Jasmine unit test case for $routeChangeStart in AngularJS

两盒软妹~` 提交于 2019-12-10 15:24:08
问题 Hi I am building an app using AngularJS and now I am on to the unit testing my application. I know how to write unit test cases for services, controllers etc. But I don't know to write it for $routeChangeStart . I have following code in my app.js app.run(function ($rootScope, $location, AuthenticationService) { $rootScope.$on('$routeChangeStart', function () { if (AuthenticationService.isLoggedIn()) { $rootScope.Authenticated = 'true'; $rootScope.Identity = localStorage.getItem(

Jasmine specs without specRunner.html - JS dependencies

空扰寡人 提交于 2019-12-10 15:18:39
问题 I have unit tests for an application where the javascript I wanted to test was dependent on other JS frameworks (underscore.js, backbone.js, jQuery...). So when setting up the specRunner.html I added these files as well: .... <head> <title>p13n.js Spec Runner</title> <link rel="shortcut icon" type="image/png" href="./jasmine-standalone-1.3.1/lib/jasmine-1.3.1/jasmine_favicon.png"> <link rel="stylesheet" type="text/css" href="./jasmine-standalone-1.3.1/lib/jasmine-1.3.1/jasmine.css"> <script