Exploring Jasmine's callThrough and callFake capabilities

One of the methods in my codebase returns function references based on different cases.

function methodToBeMocked(param){
  case 1: return func1;
  case 2: return func2;
 .
 .
 case n: return funcN;
}

Now, I need to spy on this method and provide a fake function reference for a specific input parameter p.

Is there a conditional call-through feature available in Jasmine tests? Here's my scenario:

SpyOn(someObject, 'someMethod').and.{if param = p callFake(fakeMethod) else callThrough()}

I initially tried using callFake. Is there a way to hand over control to the original method from the fake method?

Answer №1

In Jasmine testing, a spy stores the original function in a property called originalValue, allowing you to utilize it like this:

var mySpy = {};
mySpy = t.spyOn(obj, 'methodToBeMocked').and.callFake(function (param) {
    if (param === 'fake input') {
        // provide fake output
    } else {
        // execute this for Jasmine
        return (mySpy.and.callThrough())(param);
        // execute this for Ext + Siesta and confused by similar syntax :)
        // return mySpy.originalValue(param);
    }
});

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

Is there an issue with the 'shardCount' parameter in this basic Webhook code?

I am attempting to dive into coding and learn more about API integration, but I keep encountering a frustrating error in my code: const Discord = require('discord.js') const webhookID = '888151030620106792'; const webhookTo ...

Switch up the query parameter in the current Vue router route

I am working on appending attribute parameters to a URL using the following code snippet: this.$router.push({ query: Object.assign({}, this.$route.query, { attributes: this.encodedAttributes() }) }); However, I have noticed that when I call this method fo ...

BlurDataURL for Data URLs in Next.js

NextJS 11 brings the exciting feature of using the Image component with a blur placeholder. To utilize this with dynamic images, we need to make use of the blurDataURL, which is a Data URL. I am interested in taking my original image and resizing it to a ...

Receiving CORS response from the server for just one of the two API calls

Description of the issue: I am facing a peculiar problem where I am encountering different responses while making two calls to the same API. One call is successful, but the other returns a 504 error. Access to XMLHttpRequest at '' from orig ...

The Material-UI table spills out of its designated container

My material-ui table is resizing and scrolling horizontally without issue, but the table element and its contents are overflowing off the right side of the page. Check out this gif to see the behavior: https://i.stack.imgur.com/lXuHj.jpg Additionally, her ...

The promise is coming back as undefined

I am encountering an issue where the value returned from a promise is coming back as undefined in my template. In the getLabel function, I am receiving a label as a promise and resolving it before returning it to the title in the getMenuItems function. H ...

Unable to access property 'scrollToBottom' as it is undefined

I'm encountering the error "Cannot read property 'scrollToBottom' of undefined" and haven't been able to find a solution anywhere, hence this post: Here is my use case: I have a custom accordion list, and on click of one of the list i ...

Handling complex JSON data in KendoUI Grid with varying keys

I have come across a challenging issue with a complex SLC loopback query and the JSON format it returns. Despite my best efforts to find a solution, I seem to be struggling to grasp some of the answers or perhaps I am approaching the problem from the wrong ...

"Obtaining a URL that begins with using jQuery: A Step-by-

How can I extract the full URL that starts with http://sin1.g.adnxs.com Here is the code snippet: <div id="testingurl"> <div class="ads98E6LYS20621080"> <!-- This script is dynamically generated --> <iframe src="http://testing ...

Javascript text validation is malfunctioning as the alert message fails to appear

Looking for a simple form validation script: <script language=”javascript”> function checkForm(register) { if (""==document.forms.register.FNAME.value){ alert("Please fill out this field!"); document.forms.register.FNAME.focus( ...

A ServiceWorker used a promise in FetchEvent.respondWith() that resulted in a non-Response value of 'undefined'. This caused an issue in browser-sync

There are times when my server encounters an error in the console: Failed to load ‘http://localhost:3000/browser-sync/socket.io/?EIO=3&transport=polling&t=Lm2wn4p’. A ServiceWorker passed a promise to FetchEvent.respondWith() that reso ...

Localization support is working partially in a Node Express application that uses Handlebars for templating

Whenever I visit a URL with the ?lang=en query parameter, the English translation is never used. However, the Hungarian text changes work perfectly fine, displaying text to test on Hungarian in the default "hu" language without any issues. What could be ca ...

How to exclude the port number from the href in a Node.js EJS template?

I have the following code snippet. I am trying to list out the file names in a specific directory and add an href tag to them. The code seems to be working fine, however, it still includes the port number where my node.js app is running. How can I remove ...

Can javascript be used to swap out the folder in my URL?

I have been searching for solutions on how to change the language of my website using jQuery, but so far I have not found anything that works for me. Let's take my website as an example: www.domain.com I have separate folders for different languages. ...

The issue with React select right-to-left (RTL) functionality is that it

When using react select, I include isRtl as a prop like so: <Select onChange={handleChange} isRtl isMulti options={colourOptions} /> However, only the input receives the rtl direction style and not the options. How can I ensure that both the input a ...

Incorporate query strings into every hyperlink within the application

Let me paint you a picture... I've got this lovely Next.js app that's been around for a while, filled with lines of code we've poured our hearts into. Recently, we decided to spice things up by running some ads and now we are itching to see ...

angular js using ng-table in a simple example

I am currently utilizing meteor and attempting to implement the basic example provided on the ng-tables site. However, I am experiencing an issue where only the filters and sort boxes are displayed, but not the data itself. <body ng-app="myApp"> & ...

NodeJS threw an error: The call stack size has exceeded the maximum limit

My tool of choice is socket.io Error Encountered: We are facing a RangeError while calling `socket.disconnect()`; Please help resolve this issue. Thank you! ...

jQuery draggable elements can be easily dropped onto droppable areas and sorted

I need help with arranging the words in the bottom tiles by sorting them from "Most Like Me" to "Least Like Me" droppable areas. Currently, I am able to drag and drop the words into different boxes, but it ends up stacking two draggable items on top of eac ...

How can jQuery grep be used with dual arrays?

Could it be a problem with my syntax, or am I completely off track with my approach here? I'm working with two arrays that store attributes selected by the user. I'm then attempting to filter a JSON file using $.grep() to find pillows that match ...