angular-mock

How to use ngMock to inject $controller

那年仲夏 提交于 2020-01-06 15:07:16
问题 I'm trying to learn unit testing for Angular using Karma, Jasmine, and ngMock. There are at least 2 places in the Angular docs that show how to write unit tests for a controller, and I just have a couple of questions about how they're doing things. From the Controller docs, section on testing controllers: describe('myController function', function() { describe('myController', function() { var $scope; beforeEach(module('myApp')); beforeEach(inject(function($rootScope, $controller) { $scope =

Angular mock fails to inject my module dependencies

五迷三道 提交于 2020-01-02 01:07:18
问题 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 strict"; fooApp.controller('MainCtrl', function ($scope, $rootScope, fooService) { ... } So I've tested several ways to create a test, like this one: 'use strict'; describe('MainController test', function () { var scope; var controller; beforeEach(function () { angular.mock.module(

Accessing $http data in Protractor / E2E tests (AngularJS)

谁都会走 提交于 2020-01-01 12:24:01
问题 I have a bunch of Unit tests that are going well, and I've started to add Protractor E2E tests to my project. I'm doing okay testing interactive elements on the page, but I'm having trouble testing for certain data being sent out of the browser. For instance, I want to see if clicking a certain button produces a POST to a certain endpoint. I have protractor set up using the following: /*globals global*/ module.exports = function() { 'use strict'; var chai = require('chai') , promised =

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

徘徊边缘 提交于 2019-12-23 06:47:07
问题 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 spec_helper require("angular"); require("angular-mocks"); var app = require("./app"); describe("My App", function() { describe("App Controllers", function() { beforeEach(module("app")) it("Should have created an application controller", inject

Inject $log service on simple Jasmine test

天涯浪子 提交于 2019-12-22 18:18:48
问题 Yes, I'm completely new to Angular and Jasmine, and I can't figure out how to inject a mock $log for my test. This is the test: (function () { 'use strict'; describe('basic test', function(){ it('should just work', function(){ var $log; beforeEach(inject(function(_$log_){ $log = _$log_; })); $log.info('it worked!'); expect($log.info.logs).toContain(['it worked!']); }); }); }()) This fails on the inject line with an error: TypeError: Cannot set property 'typeName' of undefined What am I

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

£可爱£侵袭症+ 提交于 2019-12-17 23:46:37
问题 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) { return getContentOf("/somefile"); }); I tried to use $http, something along the lines of $httpBackend.whenPOST('/phones').respond(function(method, url, data) { return $http.get("/responses/phones.js"); }); but it didn't work, guess angular doesn't support

karma/angularjs how to test run block with service

此生再无相见时 提交于 2019-12-13 05:05:24
问题 This post follow this I've posted an other thread with a easier example code to test 'use strict'; var app = angular.module('myApp', []); app.run(function ($rootScope, Menus) { var menus = [ { 'permission': null, 'title': 'Home', 'link': 'home' }, { 'permission': 'users', 'title': 'User', 'link': 'user_actions.list' } ]; $rootScope.menus = []; function queryMenu(menus) { Menus.query(menus).then( function (result) { $rootScope.menus = result.data; }, function (reason) { throw new Error(reason)

Why am I getting this Angular-Mock error and Mocha test is not running

人盡茶涼 提交于 2019-12-13 00:49:46
问题 I'm getting the exact error as found here : (window.beforeEach || window.setup) is not a function. However the fix did not work , the author of the tutorial series here even mentioned the same fix. Here is the Tuts+ author's fix: Which is simply to place the angular-mock script tag after the Mochai and Chai scripts. Which I did so below: test/index.html <!DOCTYPE html> <html lang="en"> <head> <title>Mocha Spec Runner</title> <meta charset="utf-8"> <meta name="viewport" content="width=device

Unit Testing AngularJS and PouchDB Service

别来无恙 提交于 2019-12-12 19:19:09
问题 I am attempting to unit test my individual Angular factories but am having a hard time trying to correctly mock and inject the PouchDB object. My factory code is currently as follows: factory('Track', [function() { var db = new PouchDB('tracks'); var resource = { getAll: function() { return db.allDocs({include_docs: true}); } return resource; }]); I had tried to use Angular's $provide service to inject a mock PouchDB instance with no luck: module(function($provide) { $provide.value('PouchDB',

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

好久不见. 提交于 2019-12-12 10:34:21
问题 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 <- uiGmapGoogleMapApiProvider I can't figure out what is going wrong. Here is the reduced testcase: 'use strict'; describe('this spec', function() { beforeEach(module('uiGmapgoogle-maps')); it('tries to configure uiGmapGoogleMapApiProvider', inject(function