Questions tagged [spyon]

116 questions
2
votes
1 answer

Angular2 final version: Injected Service method under unit test returning undefined

I am trying to write some unit-tests on a component that got some services injected into it, to load the data from server. Data is loaded in this component on OnInit() method. I am trying that service method returns some dummy data, using spyOn.…
Raj
  • 127
  • 2
  • 9
2
votes
1 answer

Jasmine spyOn and how to use spies generally

I am starting out in JS unit testing and having a hard time getting my head around how to create meaningful tests with Jasmine spies. it('should take an array of shopping items', function() { spyOn(checkObj, 'getTotal'); checkObj.getTotal([1, 2,…
user1806692
  • 143
  • 1
  • 10
2
votes
1 answer

Karma error - Expected undefined to be defined

I want to unit test my controller. I started with basic test assertions of expect API. But I am facing challenge in mocking scope methods inside a conditional check. I am getting an undefined error since it is not available under scope, only the…
RVR
  • 97
  • 3
  • 13
2
votes
1 answer

SpyOn returns Expected a spy, but got Function

I am testing a controller that calls a service(through the goToPage function) in order to redirect using the spyOn. It's simple but I am getting "Expected a spy, but got Function." error. What am I doing wrong? This is my Spec: var…
Kratos
  • 1,064
  • 4
  • 20
  • 39
2
votes
1 answer

Mocking a service and calling through in Jasmine

I'm trying to test a service in my AngularJS project. All I'm trying to do is see if the method on my service has called through. I thought that when you use 'and.callThrough()' in jasmine it called the method for you and then you can see if it was…
2K01B5
  • 1,011
  • 1
  • 10
  • 23
2
votes
2 answers

spyOn: Expected a spy, but got Function

I'm creating some Javascript tests using Jasmine framework. I am trying to use the spyOn() method to ensure a specific function have been called. here is my code describe("Match a regular expression", function() { var text = "sometext";…
Nash
  • 85
  • 2
  • 8
1
vote
0 answers

Jest spy toHaveBeenCalled not seeing function as being hit

I am unit testing my JS file which consists of a lot of exported consts, with one of the functions calling some of the others held within the file, however I can't seem to get my test to see that it has done these calls - even though running the…
physicsboy
  • 5,656
  • 17
  • 70
  • 119
1
vote
2 answers

Mocking / Spying helper function in Angular Unit tests

I have a helper file with multiple function: myHelpers.ts export function fn1(a, b) { return a * b; } export function fn2(a, b) { return a + b; } export function fn3(a, b) { return a - b; } The above file, as well are others are…
Oam Psy
  • 8,555
  • 32
  • 93
  • 157
1
vote
1 answer

How to spyOn method that returns Promise -Angular

The below method calls CheckLastEmployee method which returns true/false. async nextEmployee(empId:number) { let isLast; await this.CheckLastEmployee(empId).then(x=>{isLast=x}); if (isLast) { Submit(); } } More logic inside…
PRI
  • 23
  • 5
1
vote
0 answers

jest.spyOn mock return value not returning value

The code I'm trying to test: const utils = require('../utils/utils'); let imageBuffer; try { imageBuffer = await utils.retrieveImageFromURI(params) console.log(imageBuffer) // comes back as undefined when I…
lowcrawler
  • 6,777
  • 9
  • 37
  • 79
1
vote
1 answer

How to spyOn a function that is not part of the class under test with Jest and Typescript

I'm trying to spy on a function that come from uuidv4 package but I didn't figure out how to do that. This is my User Class: import { uuid } from 'uuidv4'; import { IUser } from '../interfaces/IUser'; export class User implements IUser { …
1
vote
1 answer

SpyOn on submodule method returns no spy in jasmine

I stumble on a problem that I cannot solve. I tried to spy on a sub-modules method but got the error that the moduleSpy is not a spy with the following error message. The module is an npm package. Error: : Expected a spy, but got…
john doe
  • 11
  • 3
1
vote
0 answers

Jest SpyOn __mocks__ module

We mock our modules using __mocks__. I'd like to spyOn a module function in my test but it doesn't seem to be working. Related question, I'd also like to override a mocked module function for a test (ie throw an exception). How can I do…
jstokes
  • 211
  • 2
  • 6
1
vote
1 answer

how do I test my load function using jasmine if it has been called?

This function is reading data from a JSON file and prints it out. it not supposed to be in a class. I'm having hard time testing it if has been called. function load(nameString) { nameString = nameString.toLowerCase().split(" "); const fs =…
Sandyaxes
  • 35
  • 3
1
vote
1 answer

Unable to cover the code using Jasmine Spy

I am writing test cases for code coverage using typescript. I am using Jasmine spyOn, but it is not covered. But some of the methods are covered and I am not getting any error. I need your guidance to resolve this. I provide below the actual…
Deba
  • 859
  • 5
  • 9
  • 21