jasmine

Angular resource testing: $httpBackend.flush() cause Unexpected request

瘦欲@ 提交于 2019-12-03 04:18:20
I want to test angularjs resource. 'use strict'; /** * AddressService provides functionality to use address resource in easy way. * * This is an example usage of method: * * `get`: * var a = AddressService.get({id: '1'}, function (data) { // Work here with your resource } ); * */ App.factory('AddressService', function ($resource, $rootScope) { var url = [ $rootScope.GLOBALS.API_PATH, $rootScope.GLOBALS.API_VERSION, 'models/address/:id' ].join('/'), actions = { 'get': { method: 'GET', params: {id: '@id'} } }; return $resource(url, {}, actions); }); I created the test: 'use strict'; var

AngularJS $timeout function not executing in my Jasmine specs

别来无恙 提交于 2019-12-03 04:14:57
I'm trying to test my AngularJS controller with Jasmine, using Karma. But a $timeout which works well in real-life, crashes my tests. Controller: var Ctrl = function($scope, $timeout) { $scope.doStuff = function() { $timeout(function() { $scope.stuffDone = true; }, 250); }; }; Jasmine it block (where $scope and controller have been properly initialized): it('should do stuff', function() { runs(function() { $scope.doStuff(); }); waitsFor(function() { return $scope.stuffDone; }, 'Stuff should be done', 750); runs(function() { expect($scope.stuffDone).toBeTruthy(); }); }); When I run my app in

karma start Cannot find module 'jasmine-core'

╄→尐↘猪︶ㄣ 提交于 2019-12-03 04:12:05
I was getting the following error when I ran "karma start" module.js:340 throw err; ^ Error: Cannot find module 'jasmine-core' at Function.Module._resolveFilename (module.js:338:15) at Function.require.resolve (module.js:384:19) at initJasmine (/usr/lib/node_modules/karma-jasmine/lib/index.js:8:42) at Array.invoke [as 0] (/usr/lib/node_modules/karma/node_modules/di/lib/injector.js:75:15) at get (/usr/lib/node_modules/karma/node_modules/di/lib/injector.js:48:43) at /usr/lib/node_modules/karma/lib/server.js:137:20 at Array.forEach (native) at Server._start (/usr/lib/node_modules/karma/lib/server

Angular2 unit test with @Input()

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 04:04:24
问题 I've got a component that uses the @Input() annotation on an instance variable and I'm trying to write my unit test for the openProductPage() method, but I'm a little lost at how I setup my unit test. I could make that instance variable public, but I don't think I should have to resort to that. How do I setup my Jasmine test so that a mocked product is injected (provided?) and I can test the openProductPage() method? My component: import {Component, Input} from "angular2/core"; import {Router

Sinon JS “Attempted to wrap ajax which is already wrapped”

血红的双手。 提交于 2019-12-03 04:04:24
问题 I got the above error message when I ran my test. Below is my code (I'm using Backbone JS and Jasmine for testing). Does anyone know why this happens? $(function() { describe("Category", function() { beforeEach(function() { category = new Category; sinon.spy(jQuery, "ajax"); } it("should fetch notes", function() { category.set({code: 123}); category.fetchNotes(); expect(category.trigger).toHaveBeenCalled(); } }) } 回答1: You have to remove the spy after every test. Take a look at the example

Javascript JsTestDriver Jasmine & Jasmine-jquery

爱⌒轻易说出口 提交于 2019-12-03 03:38:47
I have lots of Jasmine unit tests, that are running unit tests for Javascripts code. They are using Jasmine-jquery plugin to do DOM manipulation. they use loadFixture, to load fixtures of HTML I tried to automate those unit tests, using JsTestDriver, with JasmineAdapter But all tests involve DOM-jquery manipulation are not passing? Is there something wrong with that? Is there a way to use Jasmine-jquery with JsTestDriver? I will answer myself because I found a solution for this problem. The problem was Jasmine-Jquery is using ajax to load the html fixture, and it uses a relative path, assuming

Error: Cannot find module 'jasmine-core'

落爺英雄遲暮 提交于 2019-12-03 03:24:38
I installed the following for testing: "devDependencies": { "jasmine-core": "^2.4.1", "karma": "^0.13.22", "karma-jasmine": "^0.3.7", "karma-phantomjs-launcher": "^1.0.0" } After running karma start I get the following error: Doing a search this is the first question with the same problem: karma start Cannot find module 'jasmine-core' However I've tried both answers, installed jasmine-core globally and I already did npm install jasmine-core --save-dev :( My test/index.html <!DOCTYPE html> <html lang="en"> <head> <title>Jasmine Spec Runner</title> <link href="testing.css" rel="stylesheet">

Spying on Backbone.js route calls with Jasmine

筅森魡賤 提交于 2019-12-03 03:05:40
Having problems spying method calls on a Backbone Router to ensure it calles the right method on a given route. excerpt from the test describe 'Router', -> beforeEach -> @router = new App.Router() Backbone.history.start() afterEach -> Backbone.history.stop() describe 'routes', -> it 'should be defined', -> expect(@router.routes).toBeDefined() describe 'default route', -> it 'should be defined', -> expect(@router.routes['']).toBeDefined() it 'should call index', -> spy = spyOn(@router, "index") @router.navigate('', true) expect(spy).toHaveBeenCalled() The router class App.Router extends

Angular2 testing: What's the difference between a DebugElement and a NativeElement object in a ComponentFixture?

倾然丶 夕夏残阳落幕 提交于 2019-12-03 03:01:27
问题 I'm currently putting together some best practices for testing Angular 2 apps on a component level. I've seen a few tutorials query a fixture's NativeElement object for selectors and the like, e.g. it('should render "Hello World!" after click', async(() => { builder.createAsync(HelloWorld).then((fixture: ComponentFixture<HelloWorld>) => { fixture.detectChanges(); let el = fixture.nativeElement; el.querySelector('h1').click(); fixture.detectChanges(); expect(el.querySelector('h1')).toHaveText(

Angular unit testing with Jasmine: how to remove or modify spyOn

吃可爱长大的小学妹 提交于 2019-12-03 02:59:39
问题 AngularJS v1.2.26 Jasmine v2.2.0 How can I change or remove the behavior of a spyOn ? When I try to override it, I get the following error: Error: getUpdate has already been spied upon var data1 = 'foo'; var data2 = 'bar'; describe("a spec with a spy", function(){ beforeEach(module('app')); var $q; beforeEach(inject(function(_updateService_, _$q_){ updateService = _updateService_; //spy the results of the getUpdate() $q = _$q_; var deferred = $q.defer(); deferred.resolve( data1 ); spyOn