Questions tagged [angular-mock]

angular-mock (or ngMock) is a module of the AngularJS framework providing features helpful for testing.

The angular-mock provides features helpful to the testing of AngularJS applications. Features include:

  • mock implementations for exception handling, logging, the http backend, and timeouts
  • facilities to help inject objects needed for tests
  • facilities to help inject mock objects which are used by the system-under-test.

To use it add angular.js and angular-mock.js to the HTML file.

209 questions
26
votes
2 answers

angular mock `module` resulting in '[Object object] is not a function'

I'm trying to create some unit tests in Angular using Jasmine being run through Teaspoon. The tests are running, however I have a simple test just to test the existence of a controller that is failing. I have the following test setup. //= require…
PaReeOhNos
  • 4,338
  • 3
  • 30
  • 41
24
votes
1 answer

What is the difference between expect and when in $httpBackend

What is the difference between $httpBackend.when('') and $httpBackend.expect('')? I don't know the difference between these two methods. Also the angularjs api doc does not help me. API documentation link:…
Jiangfei
  • 253
  • 2
  • 5
22
votes
3 answers

How to return a file content from angular's $httpBackend?

I'm trying to setup an e2e test suite in angular, and need to return canned responses using $httpBackend. It would be nice if I could just return a file content, e.g. $httpBackend.whenPOST('/phones').respond(function(method, url, data) { …
Evgeni
  • 3,341
  • 7
  • 37
  • 64
19
votes
5 answers

How to mock an angular $http call and return a promise object that behaves like $http

Is there a way to return an HttpPromise (or something similar) to mimic a call to $http? I want to set a global variable that indicates whether the real HTTP request is made or whether a fake HttpPromise object is returned with fake data. For…
whyceewhite
  • 6,317
  • 7
  • 43
  • 51
16
votes
1 answer

angular-mocks - only mock explicit request. passthrough all unexpected requests

I find it incredibly frustrating that angular-mocks blocks all requests BY DEFAULT, and forcing me to "passthrough" what I want. Sometimes I simply want to test 1 url with a mock and I have to jump through serious hoops for every "Unexpected…
parliament
  • 21,544
  • 38
  • 148
  • 238
16
votes
2 answers

How can I make $httpBackend insensitive to the order of URL query parameters?

I am using the Angular.js $httpBackend to test some services that wrap $http calls (this is in ngMock, not ngMockE2E). It seems that things like expect and when are sensitive to the order of URL query parameters. E.g. if I do…
12
votes
1 answer

How to Jasmine test code within angular module run block

I would like to Jasmine test that Welcome.go has been called. Welcome is an angular service. angular.module('welcome',[]) .run(function(Welcome) { Welcome.go(); }); This is my test so far: describe('module: welcome', function () { …
Andrew
  • 5,525
  • 5
  • 36
  • 40
12
votes
3 answers

Angular mock fails to inject my module dependencies

I want to test an Angular controller for my application fooApp, defined as follow: var fooApp = angular.module('fooApp', [ 'ngRoute', 'ngAnimate', 'hmTouchEvents' ]); ... The controller, MainCtrl is defined: "use…
Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
10
votes
3 answers

Protractor with Angular Mocks Throws "Window Not Defined"

I've searched around quite a bit on this problem, but can't find a solution. I'm trying to mock my backend, which is well tested so I can completely isolate my frontend. I've tried using protractor-http-mock and also various efforts with…
9
votes
1 answer

How to test a component with a renderer in the constructor

Consider this code: import { Component, OnInit, Renderer, OnDestroy } from '@angular/core'; import { TranslateService } from 'ng2-translate/ng2-translate'; export class AppComponent implements OnInit, OnDestroy { …
Mat Ro
  • 103
  • 1
  • 7
9
votes
2 answers

Why does this test for the angular-google-maps provider fail?

I'm trying to test a module that uses angular-google-maps. It is failing because angular.mock.inject cannot find uiGmapGoogleMapApiProvider: Error: [$injector:unpr] Unknown provider: uiGmapGoogleMapApiProviderProvider <-…
9
votes
2 answers

How to test the config function of an Angular module?

I'm defining some setup code in the config function of an Angular module that I want to unit test. It is unclear to me how I should do this. Below is a simplified testcase that shows how I'm getting stuck: 'use strict'; angular.module('myModule',…
Julian
  • 4,176
  • 19
  • 40
9
votes
1 answer

Jasmine spyOn on function and returned object

I'm using MeteorJS with angular and want to test controller. My controller use $reactive(this).attach($scope). I need to check, if this method was called. I create something like that for spy: var $reactive = function(ctrl) { return { …
psalkowski
  • 414
  • 1
  • 8
  • 20
9
votes
5 answers

Use Angular mock to load JSON file for Backendless development

I wrote this little code in separate .js file for frontend backendless environment. I need to get myfile.json whenever there is an ajax calling /somelink. angular.module('myApp') .config(function($provide) { $provide.decorator('$httpBackend',…
HP.
  • 19,226
  • 53
  • 154
  • 253
7
votes
2 answers

Unit testing AngularJS Directives with Jest

I feel I am missing something crucial in this extremely simplified angular directive unit test: import * as angular from 'angular' import 'angular-mocks' const app = angular.module('my-app', []) app.directive('myDirective', () => ({ template:…
Simon Lang
  • 40,171
  • 9
  • 49
  • 58
1
2 3
13 14