jasmine

Angular 4 unit testing with jasmine /karma with http post mocking - how to fix

最后都变了- 提交于 2019-12-04 00:55:47
问题 I have a service I want to unit test in angular 4 typescript jasmine. Now, the http is doing a post , and it returns an identity, however.. it is not sending anything. I want to just have good code coverage but i don't understand how to quite complete this mocking statement. here is the method for http post in my service file addSession() { let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); return this.http.post(this.url,

How to test XMLHttpRequest with Jasmine

妖精的绣舞 提交于 2019-12-04 00:54:06
How can I test the onreadystatechange on XMLHttpRequest or pure Javascript AJAX without jQuery? I'm doing this because I'm developing Firefox extension. I guess I have to use spies, but couldn't figure out how because my ajax won't return anything. submit : function() { var url = window.arguments[0]; var request = new XMLHttpRequest(); request.open("POST", 'http://'+this.host+'/doSomething', true); request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); request.send("param="+param+"&emotions="+this.getParams()); request.onreadystatechange = function() { if(this

Angular 2 fakeAsync waiting for timeout in a function using tick()?

隐身守侯 提交于 2019-12-04 00:30:00
I'm trying to get the results from a mock backend in Angular 2 for unit testing. Currently, we are using fakeAsync with a timeout to simulate the passing of time. current working unit test it('timeout (fakeAsync/tick)', fakeAsync(() => { counter.getTimeout(); tick(3000); //manually specify the waiting time })); But, this means that we are limited to a manually defined timeout. Not when the async task is completed. What I'm trying to do is getting tick() to wait until the task is completed before continuing with the test. This does not seem to work as intended. Reading up on the fakeAsync and

Unit testing the output of $sce.trustAsHtml in Angular

拈花ヽ惹草 提交于 2019-12-04 00:20:02
I am writing a REST app in Angular and I want to write unit tests for it (of course!). I have a controller which gets a list of blog posts from a REST service in json and puts the summaries into the $scope, so I can display them in the view. At first the blog posts were just displaying as text ie <p>Blog body</p> , rather than rendering as parsed HTML, until I discovered that you can use ng-bind-html in conjunction with the $sce service . This now works fine in terms of displaying the blog posts correctly. The problem arises when unit testing. I am trying to mock a json response with some HTML

Ionic Jasmine : env.stopOnSpecFailure is not a function after compiled successfully

喜夏-厌秋 提交于 2019-12-04 00:18:47
Using Ionic with jasmine-karma, while run test, getting success compile but in jasmine dashboard getting empty screen with error in console. Following tutorial : https://leifwells.github.io/2017/08/27/testing-in-ionic-configure-existing-projects-for-testing/ "ts-loader": "^4.1.0", "jasmine-core": "^2.99.1" Error Messages : TypeError: env.stopOnSpecFailure is not a function at adapter.js:26 Error: Module build failed: TypeError: Cannot read property 'afterCompile' of undefined The failure occurs in your version of karma-jasmine which tries to use the stopOnSpecFailure function. That function is

Jasmine date mocking with moment.js

纵然是瞬间 提交于 2019-12-04 00:16:40
I'm using moment.js for date/time in my application, but it seems like it doesn't play well with Jasmine's mocking capabilities. I've put together a test suite below that shows my issue: jasmine.clock().mockDate doesn't seem to work for moment, while it works fine for Date . describe('Jasmine tests', function () { beforeEach(function() { jasmine.clock().install(); }); afterEach(function() { jasmine.clock().uninstall(); }); // Pass it('uses the mocked time with Date', function() { var today = new Date('2015-10-19'); jasmine.clock().mockDate(today); expect(new Date().valueOf()).toEqual(today

Is there a way to verify the order of spy executions with Jasmine?

不羁的心 提交于 2019-12-03 23:40:19
I've got two objects that have been set up as spies with Jasmine: spyOn(obj, 'spy1'); spyOn(obj, 'spy2'); I need to verify that calls to spy1 come before calls to spy2 . I can check if both of them are called: expect(obj.spy1).toHaveBeenCalled(); expect(obj.spy2).toHaveBeenCalled(); but this will pass even if obj.spy2() was called first. Is there an easy way of verifying that one was called before the other? Looks like the Jasmine folks saw this post or others like it, because this functionality exists . I'm not sure how long it's been around -- all of their API docs back to 2.6 mention it,

TypeError: 'undefined' is not an object (evaluating 'currentSpec.queue.running')

本小妞迷上赌 提交于 2019-12-03 23:06:35
I am using karma + jasmine. Now thatI have tried one way of mocking a dependent factory I get this error: TypeError: 'undefined' is not an object (evaluating 'currentSpec.queue.running') at C:/test/test/client/app/bower_components/angular-mocks/angular-mocks.js:1924 at C:/test/test/client/app/bower_components/angular-mocks/angular-mocks.js:1979 at C:/test/test/client/test/spec/services/lessonplannerfactory.js:31 at C:/test/test/client/node_modules/karma-jasmine/lib/boot.js:117 at C:/test/test/client/node_modules/karma-jasmine/lib/adapter.js:171 at http://localhost:3000/karma.js:189 at http:/

Issue with angular.mock.inject method

若如初见. 提交于 2019-12-03 22:34:47
问题 I am using folloing jasmine test case 'use strict'; describe('companyService', function() { var $httpBackend, companyService; beforeEach(angular.mock.module('myApp')); beforeEach(angular.mock.inject(function(_$httpBackend_ , _companyService_) { $httpBackend = _$httpBackend_; companyService = _companyService_; })); it('should return a promise for getCompany function', function() { // expect(typeof companyService.getCompany('foobar').then).toBe('function'); }); }); i am getting the following

Babel replaces this with undefined

吃可爱长大的小学妹 提交于 2019-12-03 22:27:00
This code beforeEach(() => { this.asd= '123'; this.sdf= '234'; this.dfg= '345'; this.fgh= '456'; }); has been transpiled by Babel to: beforeEach(function() { undefined.asd= '123'; undefined.sdf= '234'; undefined.dfg= '345'; undefined.fgh= '456'; }); Why? Presumably that code is at the top-level scope of a module, and so it's in strict mode (the default for modules is strict mode), or a file that is being evaluated in strict mode (because it has "use strict" or because of Babel's defaults). The short version: If you're expecting this to be determined by beforeEach when calling your callback,