jasmine

Failed: Can't resolve all parameters for MatDialogRef: (?, ?, ?). unit testing Angular project

杀马特。学长 韩版系。学妹 提交于 2019-12-09 02:38:29
问题 I am new to angular development and more new towards unit testing using jasmine. I have created a component to sow a dialog using angular material MatDialogRef, MAT_DIALOG_DATA from @angular/material. The component is working fine but the Unit testing is giving me an error which i am not able to resolve. I really need this to work and any help would be appreciated.... Thanks in advance..!!! Please find my code below: app.module.ts import { BrowserModule } from '@angular/platform-browser';

TypeError: moment().tz is not a function

混江龙づ霸主 提交于 2019-12-09 02:06:36
问题 When testing using jasmine, I am getting this error. TypeError: moment.tz is not a function My code that I try to test is let myDate = moment().tz(undefined, vm.timeZone).format('YYYY-MM-DD'); 回答1: Fix If you're using Node.js, you may accidentally be using const moment = require('moment'); // moment instead of const moment = require('moment-timezone'); // moment-timezone Also, make sure you have installed moment-timezone with npm install moment-timezone --save Explanation The bug of requiring

Test environment in Node.js / Express application

て烟熏妆下的殇ゞ 提交于 2019-12-09 00:36:11
问题 I've just starting working with Node, and I've been following along with various tutorials. I've created an Express app, and setup Mongoose and Jasmine. How can I configure my specs so that I can: create models, automatically clean them up after each spec use a different database for creating test objects (say myapp_test) do this in a way that is as DRY as possible, i.e. not creating a before / after block with the teardown for each describe block ? 回答1: I'll try to answer you. Create models,

AngularJS ui-router: Test ui-sref

别等时光非礼了梦想. 提交于 2019-12-08 23:55:20
问题 I'm trying to test some views, that are using <a ui-sref='someState'>link</a> to link to other states in my application. In my tests I'm triggering a click on this elements like this: element.find('a').click() How do I test, if the state is switched to someState ? It would be easy, when using $state in my controller like this: // in my view <a ng-click="goTo('someState')">link</a> // in my controller $scope.goTo = function(s) { $state.go(s) }; // in my tests spyOn($state, 'go'); element.find(

How to fake return calls from the geolocator in jasmine

戏子无情 提交于 2019-12-08 23:42:19
问题 I have a function that calls the geolocator and i don't know how to test this function. I've tried spying on the geolocator and returning fake data but with no success, the original function is still used and so i would have to wait and i couldn't use mock data. // this doesn't work var navigator_spy = spyOn( navigator.geolocation, 'getCurrentPosition' ).andReturn( { coords : { latitude : 63, longitude : 143 } } ); How can I do this? 回答1: When you call the geolocation code, it looks like this

How do I create a fake NgForm object in an Angular unit test?

元气小坏坏 提交于 2019-12-08 20:07:51
问题 I have a component with a template like the following: // Template <form #f="ngForm" (ngSubmit)="onFormSubmit(f)"> <!-- A bunch of form fields --> </form> My component has a method like: onFormSubmit(form: NgForm) { this.save(); } I want to write a test that basically looks like this, testing that the save function gets called when the form is submitted: it('saves the widget when the form is submitted', () => { let testForm = new NgForm([], []); testForm.setValue({ name: "Hello", category:

unit testing angular service, beforeEach inject doesn't execute

牧云@^-^@ 提交于 2019-12-08 19:54:26
I'm trying to write unit tests for an angular service with jasmine/karma. I have a similar service test, which works just fine. But this one has some additional dependencies, is in a different module and just doesn't find the service with the inject. The service looks something like this. bService is in the same module, but commonFactory and commonService are in another module, say commonModule . (function () { 'use strict'; angular .module('myService') .service('aService', aService); aService.$inject = [ 'commonFactory', 'commonService' 'bService' ]; function aService ( commonFactory,

How to test changes on Component Bindings by parent element?

倖福魔咒の 提交于 2019-12-08 19:38:09
问题 I have a component like follows and would like to test what the $onChange method does in case the binding myBinding changes. I tried the whole morning, but could not find a way to solve this. angular .module('project.myComponent', []) .component('myComponent', { bindings: { myBinding: '<' }, template: '<div>{{$ctrl.result}}</div>', controller: myComponentController }); function myComponentController($filter, someService) { var ctrl = this; ctrl.result = 0; $ctrl.$onChange = function (changes)

How to actually reset $httpBackend expectations?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-08 19:08:45
问题 I've tried and tried to get this to work. The documentation is terse, at best: resetExpectations(); - Resets all request expectations, but preserves all backend definitions. Typically, you would call resetExpectations during a multiple-phase test when you want to reuse the same instance of $httpBackend mock. Every time my second request is called, my result always has the first result's data. Check out this fiddle http://jsfiddle.net/tbwn1gt0/2/ where I reset the expectations after the first

BeforeAll and AfterAll in javascript test cases

天涯浪子 提交于 2019-12-08 18:16:31
问题 I want do something before all tests, then after? What is the best way to organize my code? For example: backup some variables -> clear them -> test something -> restore backups. 'beforeEach' and 'afterEach' are too expencive. Thanks! 回答1: A pretty simple solution: describe("all o' my tests", function() { it("setup for all tests", function() { setItUp(); }); describe("actual test suite", function() { }); it("tear down for all tests", function() { cleanItUp(); }); }); This has the advantage