jasmine

Mock date constructor with Jasmine

泄露秘密 提交于 2019-12-19 05:03:46
问题 I'm testing a function that takes a date as an optional argument. I want to assert that a new Date object is created if the function is called without the argument. var foo = function (date) { var d = date || new Date(); return d.toISOString(); } How do I assert that new Date is called? So far, I've got something like this: it('formats today like ISO-8601', function () { spyOn(Date, 'prototype'); expect().toHaveBeenCalled(); }); See: https://github.com/pivotal/jasmine/wiki/Spies 回答1: Credit

Angular Jasmine Integration Test with service using real HTTP

蹲街弑〆低调 提交于 2019-12-19 04:55:34
问题 [Angular version: 2.4.5] I'm attempting to write an Angular integration component unit test that contains a service that leverages the built-in Angular HTTP library. I cannot get my Service class to instantiate though: either the Service is undefined or I get "Error: Cannot resolve all parameters for 'RequestOptions'(?)". I understand that typically you'd mock the backend (have successfully done that in other unit tests) or use Protractor. Other SO questions reference Angular 1 or have

Why is Karma refusing to serve my JSON fixture (which I'd like to use in my jasmine / angularjs tests)

主宰稳场 提交于 2019-12-19 03:38:23
问题 As indicated in this stackoverflow answer, it looks like Karma will serve JSON fixtures. However, I've spent too many hours trying to get it to work in my environment. Reason: I'm doing angular testing and need to load mock HTTP results into the test, as Jasmine doesn't support any global setup/teardown with mock servers and stuff. In my karma config file, I'm defining a fixture as so: files: [ // angular 'angular/angular.min.js', 'angular/angular-route.js', 'angular/mock/angular-mocks.js', /

Karma jasmine tests: Highlight diff in terminal

♀尐吖头ヾ 提交于 2019-12-19 01:40:14
问题 I'm using Karma with Jasmine for my tests. In some tests, I have large objects that the test relies on. When I do something like expect(obj).toEqual(expectedObj); and obj != expectedObj , I get an error message in my terminal. But this error is really long , because it includes both of the objects, and it's very hard to see, in what parts the two objects are different. So, is there any highlighter for the terminal, that can be used along with karma? This way, it would be much more easy to

Angular Jasmine UI router inject resolve value into test

♀尐吖头ヾ 提交于 2019-12-18 20:29:40
问题 In my Angular app, UI router resolves a promise into the controller. When trying to test this controller, Karma is complaining about an unknown provider. How do I inject a fake object into the test to represent this resolve object. My app's code looks something like: angular.module('myapp') .config(function($stateProvider, $urlRouterProvider) { $stateProvider .state('tab.name', { ... resolve: { allTemplates: function(Templates) { return Templates.all().then(function(templates) { return

Custom Jasmine reporter in Protractor tests

百般思念 提交于 2019-12-18 18:51:29
问题 I can not find how to change reporter style in protractors runner using jasmine framework. What I have right now is: But I would like something more like: Is there a way to add custom reporter for jasmine that would show current test running instead of DOTS and Fs? 回答1: I am building a jasmine reporter that does exactly what you want, jasmine-spec-reporter. 回答2: Add the isVerbose flag to the protractor config, it's false by default: exports.config = { . . . // Options to be passed to Jasmine

Using Jasmine to spy on variables in a function

寵の児 提交于 2019-12-18 18:46:26
问题 Suppose I have a function as follows function fun1(a) { var local_a = a; local_a += 5; return local_a/2; } Is there a way to test for the value of local_a being what it should be (for example in the first line of code)? I'm a bit new to Jasmine, so am stuck. Thanks in advance. 回答1: Not really. You can do the following though: Test the result of fun1() : expect(fun1(5)).toEqual(5); Make sure it's actually called (useful if it happens through events) and also test the result: var spy = jasmine

How do I provide re-usable sample data values to my angularjs / jasmine unit-tests

拜拜、爱过 提交于 2019-12-18 16:50:28
问题 I would like to provide simple constant values such as names, emails, etc to use in my jasmine unit tests. A similar question was asked here: AngularJS + Karma: reuse a mock service when unit testing directives or controllers In c# I would create a static class to hold little snippets of mock data. I can then use these values throughout my unit tests, like this: static class SampleData { public const string Guid = "0e3ae555-9fc7-4b89-9ea4-a8b63097c50a"; public const string Password = "3Qr}b7

How do I mock RxJs 6 timer?

不问归期 提交于 2019-12-18 15:51:24
问题 We've recently updated from Angular 5 to Angular 6, and with it RxJs 6. As part of the migration, the timer usage has changed from: Observable.timer() to timer() There are a number of places in our tests where we mock timer observables with the following pattern. let timerObserver: Observer<any>; beforeEach(() => { spyOn(Observable, 'timer').and.returnValue(Observable.create( ((observer: Observer<any>) => { timerObserver = observer; }) )); }); it(`should not have any notifications by default`

Jasmine - how to spyOn instance methods

不问归期 提交于 2019-12-18 14:16:34
问题 I have a function var data = {}; var myFunc = function() { data.stuff = new ClassName().doA().doB().doC(); }; I'd like to test that doA , doB , and doC were all called. I tried spying on the instance methods like this beforeEach(function() { spyOn(ClassName, 'doA'); }; it('should call doA', function() { myFunc(); expect(ClassName.doA).toHaveBeenCalled(); }); but that just gives me a "doA() method does not exist" error. Any ideas? 回答1: Where you went wrong was your understanding of how to