Jasmine secretly observes the behavior of Angular services

I'm currently in the process of setting up unit tests for an angular controller using jasmine and karma. Due to the length of my code, I won't be able to include it all here. However, I'll provide some snippets:

CompilerController.js (the controller I aim to test)

(function() {
    angular.module('webcompiler')
        .controller('CompilerController', ['templateService', 'compilationService', CompilerController] );

    function CompilerController(templateService, compilationService) {
        var vm = this;

        /// Initialization
        (function() {
            vm.template = 'c_basic.c';
            ...
        })();

        /// Public members
        vm.loadTemplate = loadTemplate;

        /// Implementation
        function loadTemplate() {
            templateService.get(vm.template, function(source) {
                vm.sourcecode = source;
            });
        }
    }
})();

CompilerController.spec.js

 describe('CompilerController', function() {
    var CompilationService, TemplateService, controller;

    beforeEach(function() {
      module('webcompiler');
    });

    beforeEach(function() {
      inject(function(_$controller_, _TemplateService_, _CompilationService_) {
        CompilationService = _CompilationService_;
        TemplateService = _TemplateService_;
        spyOn(TemplateService, 'get').and.callFake(function(code, callback) {
          callback('c_basic_content');
        });
        controller = _$controller_('CompilerController');
      });
    });

    it('starts with default template as source', function() {
      expect(controller.template).toBe('c_basic.c');
      expect(controller.sourcecode).toBe('c_basic_content');
    });

    describe('loadTemplate function', function() {
      it('changes the content of the source area when called', function() {

        spyOn(TemplateService, 'get').and.callFake(function(code, callback) { 
          if(code == 'c_parameters.c') { callback('c_parameters_content'); }
        });
        controller.template = 'c_parameters.c';
        controller.loadTemplate();
        expect(controller.sourcecode).toBe('c_parameters_content');
      });
    });
  });

Despite uncertainty about changing spies after controller creation, all tests fail with the following error message:

PhantomJS 1.9.8 (Windows 7 0.0.0) CompilerController loadTemplate function changes the content of the source area when called FAILED Error: [$injector:unpr] Unknown provider: TemplateServiceProvider <- TemplateService

Here is a snippet from my karma.conf.js :

module.exports = function (config) {
    config.set({
        // base path, that will be used to resolve files and exclude
        basePath: '../../',
        // testing framework to use (jasmine/mocha/qunit/...)
        frameworks: ['jasmine'],
        // list of files / patterns to load in the browser
        files: [
            // bower:js
            'main/webapp/bower_components/jquery/dist/jquery.js',
            'main/webapp/bower_components/angular/angular.js',
            'main/webapp/bower_components/angular-cache-buster/angular-cache-buster.js',
            'main/webapp/bower_components/bootstrap/dist/js/bootstrap.js',
            'main/webapp/bower_components/angular-mocks/angular-mocks.js',
            // endbower
            'main/webapp/js/webcompiler.module.js',
            'main/webapp/js/TemplateService.js',
            'main/webapp/js/CompilationService.js',
            'main/webapp/js/CompilerController.js',
            'test/javascript/**/*.coffee' // Using coffeescript for specs. The file shown above are compiled versions.
        ],
        // Browser on which to test
        browsers: ['PhantomJS'],
        // Compile coffeescript specs
        preprocessors: {
          'test/javascript/**/*.coffee': ['coffee']
        },
        coffeePreprocessor: {
          options: {
            bare: true,
            sourceMap: false
          },
          transformPath: function(path) {
            return path.replace(/\.coffee/, '.js')
          }
        }
    });
};

Answer №1

In the controller, you are injecting the service "templateService" in lowercase but it is injected in uppercase inside the beforeEach function. Perhaps adjusting this will solve the issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble understanding how to display an HTML file using Next.JS?

Currently, I am working on a project that involves utilizing Next.JS to develop a webpage. The main goal is to display a PDF file on the left side of the screen while integrating Chaindesk on the right side to create a chat bot capable of extracting inform ...

What is the best way to sort my API responses to display only users who are either currently online or offline?

Hi everyone, I've made great progress on my project so far without any assistance (pretty proud of myself), but now I could use some help. I'm working on creating a tabbed menu that filters the results of my API calls to display: All users, Onlin ...

The button click function is failing to trigger in Angular

Within my .html file, the following code is present: The button labeled Data Import is displayed.... <button mat-menu-item (click)="download()"> <mat-icon>cloud_download</mat-icon> <span>Data Imp ...

Utilizing DataTables and Ajax call to refresh table data with Json response

My table is dynamically generated using Thymeleaf and I want to update its contents with jQuery. <table class="table table-hover" id="main-table"> <thead class="thead-inverse"> <tr> <th class="c ...

Conceal a division on a webpage according to the URL of the specific

<script> function hideSearchField() { if (/menu/.test(window.location.href)) { document.getElementById('searchfield').style.display = 'none'; } } hideSearchField(); </script> I am trying to accomplish this using Jav ...

Sorting data based on column names

New to Angular and looking to create a table displaying news items in different categories. Each news item will have a category, heading, subheading, and introduction, with one column for each category: US International Head1 Head1 Sub1 Sub1 ...

A controller in Angular.js that leverages several different services

I'm having trouble injecting multiple services into a controller. Here are my listed files: index.html file: <script src="'./angular/angular.min.js'></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0-beta ...

What is the best way to implement switchMap when dealing with a login form submission?

Is there a better way to prevent multiple submissions of a login form using the switchMap operator? I've attempted to utilize subjects without success. Below is my current code. import { Subject } from 'rxjs'; import { Component, Output } ...

Is there a way to reverse the hover effect on div elements?

Let's start by examining the code I've written: HTML: <div class="button_container"> <div class="inner_button"> <a href="#" class="button_text">Button</a> </div> <div class="button_side"> ...

Updating the progress bar without the need to refresh the entire page is

Currently, I have two PHP pages: page.php and loader.php. Loader.php retrieves data from MySQL to populate a progress bar, while page.php contains a function that refreshes loader.php every second. This setup gets the job done, but it's not visually a ...

I aim to design a unique child window specifically for an "about" section within an electron js application on the Windows platform

I am looking to create a child browser window to showcase some key points about my application. According to the Electron JS documentation, it supports the "about" role for Mac OS but does not have built-in support for Windows. Therefore, I am in the pro ...

The environmental variables stored in the .env file are showing up as undefined in Next.js 13

I am having trouble accessing the environment variables stored in my .env.local file within the utils folder located in the root directory. When I try to console log them, they show as undefined. console.log({ clientId: process.env.GOOGLE_ID, clien ...

React JS component experiencing issues with Material UI modal functionality

Attempting to reproduce the material ui modal example has proven to be a challenge for me. Initially, I encountered an error stating "Cannot read property 'setState' of undefined" which I managed to resolve. However, even after resolving this iss ...

The AngularJS Factory $http encounters an error when attempting to read the property 'length' of an undefined value

I am looking to implement a factory in my REST Controller that will return an array of Strings. I want to be able to reuse this function in my services.js file. Here is the HTML for an Autocomplete input field: <input type="text" ng-model="vertrag.ver ...

NextJS hot reload with Docker is a powerful combination for seamless development environments

I've encountered issues trying to configure hot reload with Docker and NextJS. When I make changes and save a file, the server does not reload. Below is the contents of the docker-compose.yml: version: '3' services: mainapp: build: ./ ...

The like count displayed in Django's AJAX feature is identical for every post

Having an issue with the ajax setup in my django twitter clone app. Every post's like count remains the same after clicking the like button, but it gets updated after a page refresh. I'm close to fixing it, but currently stuck. View: def add_li ...

The Google Maps API has been successfully initialized, however, it is experiencing difficulties being displayed on the webpage

I have been successfully using a custom Google API on various web pages. However, I encountered an issue where the map API loads successfully but is not displaying on a specific web page. Below are the relevant code snippets: <html> <head> &l ...

How to Utilize Custom Component Tag Names in CSS with Vue.js 2?

<template> <header> <hamburger></hamburger> <app-title></app-title> <lives></lives> </header> </template> <script> export default { name: 'Titlebar& ...

If the PHP variable evaluates to true, then a CJuiDialog in Yii view will open

I am attempting to open the CJuiDialog if the $check value equals true in the view part. <?php if($check==true) { js:function(){$('#dialogDisplay').dialog('open');} } $this->beginWidget('zii.widgets.jui.CJuiDialog', ...

Using Rails Devise as a backend to validate users in an Ionic application

Is anyone experienced with setting up authentication for an Ionic app connected to a Rails API using Devise? If so, I'd love to hear about your implementation and any helpful tutorials you've used. Please share your insights. ...