简体   繁体   中英

Jasmine and Spying/Mocking - JavaScript Prototypes

Alrighty so here's the breakdown: I have a file called FileA.js. Within FileA.js I have a prototype FileAObject.prototype, with the associated function funcAlpha(). Thus we have something like this:

File = FileA

function someFunction() {
    SomeFunctionality...
}

function FileAObject() {
    Object definition
}

FileAObject.prototype.funcAlpha = function() {
    ...
}

I would like to spy on funcAlpha(). From what I know, a typical mock looks like this:

var FILE_A = $.import('path.to.file.directory', 'FileA');

<rest of code here>

spyOn(FILE_A, 'funcAlpha').andCallFake(function() {
    return fakeResult;
}

<complete test>

Now when I run my test, this doesn't work. Because funcAlpha is an attribute of FileAObject and (apparently) not FileA, the call won't work. However, I don't know how to get at the object for spying. I'm very new to JavaScript and this is a useful but fairly confusing subset of problems. Any help would be greatly appreciated!

Alrighty so it comes down to how multiple object types are contained within the file. Because FileAObject is effectively an attribute of the FileA.js file, we need to call it explicitly.

var FILE_A = $.import('path.to.file.directory', 'FileA');

<rest of code here>

spyOn(FILE_A.FileAObject.prototype, 'funcAlpha').andCallFake(function() {
    return fakeResult;
}

<complete test>

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