The module.run function in Angular is invoked with each individual unit test

Hey there

I am currently working on setting up jasmine-karma unit tests for my angular app. The problem arises in my app.js file where the module.run method is calling a custom service (LoginService) that then makes a call to the $http service. The issue I'm facing is that I am testing the LoginService and mocking the $http service, but when I try to flush, it gives me this error:

 Error: Unexpected request: GET /frontend/login
 No more request expected

I suspect that this additional request is generated by the module.run function.

 app.run(['LoginService',function(LoginService){

     LoginService.checkLoginStatus().then(function(status){
         console.log(status);

       }, function(status){

           console.log(false);

      });

}]);

You can check out my code on Plunkr here: http://plnkr.co/edit/f9bEH1fNTxDoxvWfajH5

Answer №1

I encountered an issue where, during my unit test, I was initializing the entire application

 beforeEach(module("app")).

After organizing my controllers, directives and services into their own modules

 beforeEach(module("app.controllers"))
 beforeEach(module("app.directives"))
 beforeEach(module("app.services"))

The problem was resolved once I made this change. It turned out that my test setup was not for unit testing, but rather for end-to-end testing.

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

What are the steps for defining a package once and utilizing it across multiple pages?

Hello, I'm new to Next.js and I have a question regarding the code organization. In my project, I have two pages: PostList (index.js) and PostSingle ([postId].js). I want to implement a feature that allows users to switch between dark and light theme ...

`Having trouble with importing the react-redux module in next.js due to an error with the <Provider/&

Encountered the following error in Next.js browser and terminal: SyntaxError: Cannot use import statement outside a module import Provider from './components/Provider'; ^^^^^^ SyntaxError: Cannot use import statement outside a module at Obj ...

The MODULE is malfunctioning due to an unidentified mutation type: user/setUserInfo

My store/index.js looks like this: const store = new Vuex.Store({ modules: { app, user, }, state: { token: getToken(), hasLogin: false, }, mutations: { ...rootMutations, }, actions: { ...rootActions, }, }) export de ...

After refreshing the page in Next JS, there is a delay in loading the Swiper Js styles. The Swiper slides appear stretched while waiting for Next JS to load the styles. Any suggestions

Having an issue with my Next 14.0.3 app and tailwind CSS. I recently installed swiper JS version 11.0.5 using npm. The problem arises when I reload the page, it takes about 1 or 2 seconds for the swiper styles to load. During this time, the swiper slides s ...

The JQuery get method fails to send a request to the server

When running the following jQuery code to load a partial page onto a hidden div, everything works fine on my local machine. However, once I add the code to the server, the partial page does not load. Any suggestions on why this might be happening? var u ...

Efficiently tracking online users in Sails.js

As a beginner in sails.js (node.js), I'm trying to figure out the most effective method for determining the current number of users online. Any insights or suggestions? ...

Looking to optimize this code? I have three specific tags that require reselection within a given list

for (var i=0; i<vm.tags.length; i++) { if (selectedTags[0]) { if (vm.tags[i].term_id === selectedTags[0].term_id) { vm.tags[i].border1 = true; } } if (selectedTags[1]) { if (vm.tags[i].term_id === selecte ...

Narrow down product selection by multiple categories

I'm currently in the process of working with Express and MongoDB, where I have data items structured like the following: { "_id": { "$oid": "63107332e573393f34cb4fc6" }, "title": "Eiffel tower&quo ...

The self-made <Tab/> element is not functioning properly with the ".Mui-selected" class

I have customized a <Tab/> element and I want to change its selected color using Sandbox demo code export const Tab = styled(MuiTab)({ "&.Mui-selected": { color: "red" } }); However, I've noticed that: 1. Apply ...

"I'm experiencing an issue where my JSON data is not displaying in the browser when I run the code

I am having trouble displaying my json data in the browser. This is my first time working with json and I can't seem to identify the issue. I tried researching online and found that it could be related to mime types, but I still can't solve it. B ...

What is the best way to add an array inside an object?

Currently, I am in the process of developing an application using a combination of SailsJS, AngularJS, and MongoDB. One of the challenges I am facing is figuring out how to properly insert a data array into an object within my schema. The Example Schema ( ...

Sort Ionic Expandable List

Currently, I am in the process of creating an application using Ionic framework. One feature in my app involves displaying a list with professions and their corresponding specialties in an accordion format, as demonstrated below: Profession 1 Specialty ...

Conditions in Controller Made Easy with AngularJS

I have recently started working on implementing a notifications feature. The service will involve making a GET request to a specific URL which will then return an array of notifications. Within the controller, I am in the process of setting up a variable ...

There seems to be an issue with the function code error when calling it within the

When I attempt to run my code in this way, I encounter a compile time error stating that the expression statement is not an assignment or call... (within the else statement). What am I missing here to get it to work? I've made numerous attempts to ad ...

The issue lies in AngularJS where updating data that is bound does not automatically update the corresponding template

Currently, I am in the process of building my very first mobile app using a combination of html5, cordova, angularjs, and jQuery. Initially, I had no issues creating my controller and template. The data that I bind to the template is retrieved from a .net ...

React Pagination Component not displaying on User Interface

Implementing pagination in my react application has been a challenge for me. I followed this guide to create the Pagination.js file, but unfortunately, I can't see it reflected on my UI. You can view a screenshot of my application https://i.sstatic.ne ...

Tips for properly formatting the data before sending it to an API

When a user submits a form, the data is sent to an API. It's important that every field appears on a new row in the output. Unfortunately, using \n and <br> inside the string does not produce the desired formatting. Can you help me with thi ...

What is the best way to update a div element following a successful AJAX request?

Sharing my experience as I struggled to find a raw JavaScript solution among all the jQuery answers. I have a function that posts comments to a section, and I want the section to refresh after posting. However, my code seems to be failing to work as expec ...

Angular's Components and Directives: A Comprehensive Guide

My goal is to render a subview within a template and define the state inside the subview's controller when an element is clicked. I am separating it from the main controller because there will be further subviews within this initial subview. However, ...

Comprehending the significance of *this* within class structures

I've got this code snippet below. class Node { constructor(value, parent, possibleChildren = []) { this.value = value; this.parent = parent; this.children = [] this.setChildren(possibleChildren); } setChildren(possibleChil ...