Unable to access the newly created object's properties following the instantiation of a new resource in AngularJS

Currently, I am in the process of developing a new Resource utilizing AngularJS that falls under the category of Person. After successfully creating this resource, my goal is to retrieve the id associated with the new resource from the server.

it('should obtain the latest Person id post addition', inject(function($httpBackend, Person) {
        $httpBackend.whenPOST('http://api.example.com/Persons').respond({ "id" : 32});

        var newPerson = new Person();
        newPerson.$save();
        expect(newPerson.id).toEquals(32);

 }));

Upon running my code in Karma, I encounter an error message stating "Expected undefined to be 32."

I believed that my code excerpt closely resembled the example provided in the $Resource documentation, specifically at the conclusion of the Credit Card code snippet.

Could you offer any insights as to where I may have gone wrong?

Answer №1

Remember to use $httpBackend.flush() in order to clear pending requests.

Give this a try:

it('should retrieve the updated Person ID after addition', inject(function($httpBackend, Person) {
        $httpBackend.whenPOST('http://api.example.com/Persons').respond({ "id" : 32});

        var newPerson = new Person();
        newPerson.$save();

        $httpBackend.flush();
        expect(newPerson.id).toEquals(32);
 }));

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

Access to this page via the Odesk API in node.js is restricted and unauthorized

/** * Here is an example of how to use the oDeskAPI * * @package oDeskAPI * @since 09/22/2014 * @copyright Copyright 2014(c) oDesk.com * @author Maksym Novozhylov <<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data ...

I am experiencing difficulties with displaying my array of JSX elements in the render function of my ReactJS application. What could be

I am currently working on a trivia application and encountering an issue with inserting an updated array of "Choice" elements for each question. Despite my efforts, whenever I attempt to insert an array of JSX elements, the array appears blank. This is qui ...

Drop and drag the spotlight

On my website, I am looking to implement a feature that will make it easier for users to identify the drag and drop area. I found a code snippet on JSFIDDLE that works perfectly there. However, when I tried to use it on my local server, it doesn't se ...

Utilize local JSON file to display images on a webpage using AngularJS

Where am I making a mistake? I am trying to display images from a datahl.json file on my web page, but for some reason, I am unable to access them. Please review the code snippets below and help me out. If possible, provide a solution using Plunker edito ...

transferring data from ejs template

In my app.js file, I have an array that stores files. To display these files on a website, I use a for-loop in ejs: <% for(let i = 0;i<posts.length; i++){ %> <li class="listtitle"> <%= posts[i].title %> </li> ...

Click to switch CodeMirror's theme

http://jsbin.com/EzaKuXE/1/edit I've been attempting to switch the theme from default to cobalt and vice versa, toggling each time the button is clicked. However, I am facing an issue where it only switches to the new theme once and doesn't togg ...

When the down key is pressed in a textarea, choose the list item

I have HTML similar to the following <div class="row"> <textarea id="txtArea" ng-model="input" ng-change="handleOnChange(input);getSearchField(input);" ng-click="search(input)" ng-focus="search(input);" ...

The enigma of AngularJS

It seemed like I was starting to understand AngularJs, but now I realize I still have a lot to learn... I attempted to create a simple demo showcasing the ng-repeat directive, and suddenly nothing is working - all I see is a blank screen with no explanati ...

Modifying a gridview cell through a Modal popup that is displayed using the rel attribute

I have successfully implemented a modal dialog using CSS and the rel="#showEditModal" attribute of a button. This enabled me to add values to the database and update the gridview effectively. However, I am now facing a challenge where I need to be able to ...

Unable to display image on HTML page in Sails JS

In my current project, I am utilizing Sails Js and Mongo DB for development. When a user uploads an image and content for a blog post, I store it in the images folder and save the file destination along with the content to MongoDB. My goal is to display bo ...

Incorporating a JavaScript npm module within a TypeScript webpack application

I am interested in incorporating the cesium-navigation JavaScript package into my project. The package can be installed via npm and node. However, my project utilizes webpack and TypeScript instead of plain JavaScript. Unfortunately, the package is not fou ...

Is there a way to access a component based on the parameter in the Vue router?

I am working on a Vue component called Portfolio.vue, which contains a child component called Category.vue. I am able to navigate to the Category.vue component using <router-link :to = "{ name: 'category', params: { id: id }}"> wh ...

Dealing with query strings within routeprovider or exploring alternative solutions

Dealing with query strings such as (index.php?comment=hello) in routeprovider configuration in angularjs can be achieved by following the example below: Example: angular.module('app', ['ngRoute']) .config(function($routeProvider, $loc ...

Using ngFor and ngModel: What is the best way to add values to the beginning of the array I am looping through?

I am facing an issue with my array of elements in which users can edit, add, and delete complete array elements. Everything works smoothly until I try to add a value to the beginning of the array using the unshift method. Below is a test that showcases th ...

Generate an asynchronous boolean promise when calling the isPresent function in Protractor

I'm currently working on a function that checks the presence of an element and returns a boolean value accordingly. However, I am facing an issue where the response includes unnecessary information. the output displays: false How can I adjust my cod ...

Looking for specific data in AngularJS using a filter

I'm facing a situation where I have to search based on filtered values. Below is the code snippet var app = angular.module('MainModule', []); app.controller('MainCtrl', function($scope) { $scope.searchText = '& ...

Tips for preserving selection state in a multi-page results display?

In the process of developing a web application, I am working on implementing a way to save selection states. Specifically, I am building a query interface that interacts with a MongoDB backend. The search results are displayed in a grid format with check ...

implement a Django for loop within the template by utilizing JavaScript

Is it possible to incorporate a {% for in %} loop and {{ variables }} in a Django template using JavaScript DOM (insertAdjacentText, or textContent) and dynamically load data from the views without refreshing the entire page? If so, can you please guide me ...

Displaying a PHP variable on the console through the power of jQuery and AJAX

I'm attempting to display the value of a PHP variable in the browser's console using jQuery and AJAX. I believe that the $.ajax function is the key to achieving this. However, I am unsure about what to assign to the data parameter and what should ...

Exploring Twig variables in Node.js with the node-twig package

Despite following the documentation meticulously, and experimenting with various methods, I am still unable to achieve success. I have attempted using the code snippet below, trying to reference the variable in the main file like this: // None of the opti ...