jasmine

Unable to test a rejected promise after migrating to Angular 1.6.3

冷暖自知 提交于 2019-12-11 06:48:30
问题 I have recently updated my application from Angular 1.5 to 1.6.3 and started getting Jasmine unit test failures (with PhantomJS) around promise based code I have written: Possibly unhandled rejection: undefined thrown Reading around I see that the accepted solution is to chain .then() with .catch() blocks to handle the rejections gracefully. I have done this for one of my source files that I am testing to prove this gets past the error which it does. However, it has now uncovered a further

Unit test an angular controller and service which uses a promise?

瘦欲@ 提交于 2019-12-11 06:37:31
问题 I cannot get the test result to pass I'm using a very basic implementation to understand testing deeper. I have a factory which returns a promise, accessed from my controller. I want to test that the call succeeds and assigns the response to the repos var. Following is the code: 'use strict'; angular.module('app') .factory('searchServ', function ($timeout, $q, $http) { return { fetch: function(user) { var deferred = $q.defer(); $timeout(function(){ $http({method: 'GET', url: 'https://api

Component declaration in spec files as opposed to loading the top-level module into TestBed

送分小仙女□ 提交于 2019-12-11 06:07:07
问题 I have an Angular (4.x) application with a top-level module AppModule , which declares several custom components, so they can be used in templates. However, for jasmine/karma tests the most common approach seems to be using BrowserDynamicTestingModule and declare any required custom components during beforeEach , e.g.: beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ MyComponent, SomeDependedUponComponent ] }) .compileComponents(); })); Considering we're simulating

this.ProductDataService.getAllProducts is not a function using jasmine-karma

血红的双手。 提交于 2019-12-11 06:06:43
问题 I am running unit test-cases i.e. jasmine-karma in angular7. And I am getting this error - ProjectManagementComponent should use the ProjectList from the service TypeError: this.ProjectManagementService.getProject is not a function If instead of useValue , i use useClass , i get error - [object ErrorEvent] thrown I tried varied options , but unable to figure out over internet. app.component.spec.ts describe('ProjectManagementComponent', () => { let comp: ProjectManagementComponent; let

How to test window.load() in Jasmine with a specific window.location.href?

无人久伴 提交于 2019-12-11 05:45:22
问题 I have the following code: 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'); } }); } }; And I am trying to write an appropriate test in Jasmine. So, far I have the following code: describe("should disable button", function () { it("should check if book exists in list while loading

Testing backbone views with jasmine

允我心安 提交于 2019-12-11 05:21:40
问题 I am trying to test my backbone views with jasmine. I use underscore library to generate templates for the backbone views. For testing purposes I use jasmine jasmine-jquery I am unable to load the fixtures in the jasmine tests as the views have embeded ruby. Here is my code Backbone view AlbumView = Backbone.View.extend({ template: _.template($('#album-template').html()), render: function() { $('#albums').append(this.template(this.model.attributes)); } }); This view uses the following

jasmine not working with puppeteer

笑着哭i 提交于 2019-12-11 05:17:46
问题 I'm trying to run a simple test in jasmine using puppeteer, however I can't get puppeteer to work when i use it in my test script: const puppeteer = require('puppeteer'); describe("Jasmine puppeteer", function() { let browser; let page; beforeAll(() => { browser = await puppeteer.launch({headless: false}); page = await browser.newPage(); await page.goto('chrome://newtab'); await page.screenshot({path: 'a.png'}); }) it("jasmine puppeteer", () => { expect(await page.title()).toBe(""); done(); }

How to write a jasmine unit test case to test the event handlers in angularjs

♀尐吖头ヾ 提交于 2019-12-11 05:14:36
问题 I am currently writing a jasmine unit test case to test the button's click event. I have written the following test cases for modal popup, please find my plnkr here. I have a button called Add. If the user clicks the button a modal popup opens. So I want to write a test case to find whether the modal is displayed when the user clicks the add button. How do I do it? Thanks, Varun Krishna. P 回答1: First of, as with all unit tests, make sure that you're not mixing N tests into one - that always

Testing Jquery selector with Jasmine unit tests

好久不见. 提交于 2019-12-11 05:11:20
问题 $('.froala > div > div > div > p').each(function (index, element) { if ($(element).text() !== '') { wordCount += $(element).text().split(' ').length; } }); I have this code that gets all the < p > tags in the froala editor and counts them. I need to write a Jasmine unit test to cover this and I don't have a clue how to do that. Maybe I could use a spyOn and return an array of < p > tags... spyOn($('.froala > div > div > div > p'), 'each').and.returnValue([all, my, tags, here]); Any other

how to simulate keypress for unit testing in jasmine

大憨熊 提交于 2019-12-11 05:06:38
问题 I need to unit test function that is triggered when key pressed. public onKeyDown(event: KeyboardEvent): void { if (event.ctrlKey && event.keyCode === 38) { console.log('increase'); } if (event.ctrlKey && event.keyCode === 40) { console.log('decrease'); } /* Prevent entering characters */ if (event.keyCode >= 65 && event.keyCode <= 90) { return; } } How can I simulate keypress to satisfy the fist condition, for example? 回答1: The example code below shows how an event is created, triggered, and