简体   繁体   中英

How to create a spy variable in a function in javascript using jasmine

I've created a function for this input field. I'm really stuck with on how to write test cases for this function using jasmine. The argument in the function changes depending on the input field it is on. (Code Below for Reference)

<input type="text" class="ipClass" onfocusout="return 
        outputFunction('uniqueIdOne') id="uniqueIdOne">

<input type="text" class="ipClass" onfocusout="return 
        outputFunction('uniqueIdTwo') id="uniqueIdTwo">


function outputFunction(id){
var inputValue = document.getElementby(id).value;
if(value == "10")
  return inputValue;
else
  return somethingElse;
}

Is that function a property of an Object? Becasue as I see, you can create spies for an object's functions.

Eg:

 function Human(firstName, lastName) {
  this.firstName = firstName,
  this.lastName = lastName,
  this.fullName = function() {
    return this.firstName + " " + this.lastName;
 }
}

here you can spy by having

 var human = new Human('some', 'name');
 spyOn(human, "fullName");

and then

 expect(human.getName).toHaveBeenCalled();

Please refer "In Jasmine, mocks are referred to as spies. There are two ways to create a spy in Jasmine: spyOn() can only be used when the method already exists on the object, whereas jasmine.createSpy() will return a brand new function:"

but if you're just wanting to know on how to write a spy in Jasmine. It's the same link as above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM