Last active
November 23, 2022 02:05
-
-
Save ecowden/4628658 to your computer and use it in GitHub Desktop.
Use the Jasmine spy's mostRecentCall element to get the arguments of a test invocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Let's say we have a collaborater with a 'show' function. The instance under test is going to invoke | |
* this function and pass two arguments: 'parameters' and a callback function. We often want to either | |
* do some inspection on the 'parameters' to make sure they're right, or invoke the callback to verify | |
* behavior after the callback, such as after an XHR returns. | |
*/ | |
describe("someFunctionThatInvokesOurCollaborator", function () { | |
var instance, | |
TemplateResource; | |
beforeEach(function () { | |
//create a spy with a 'show' function | |
TemplateResource = jasmine.createSpyObj("TemplateResource", ["show"]); | |
// assign the spy to the instance (it probably won't look like this) | |
instance = new MyObjectUnderTest(TemplateResource); | |
// excercise the behavior under test | |
instance.someFunctionThatInvokesTemplateResourceDotShow(); | |
}); | |
it("should pass the correct parameters", function () { | |
// verify that our first 'parameters' argument is correct | |
var args = TemplateResource.show.mostRecentCall.args; | |
expect(args[0]).toEqual("theRightParameters"); | |
}); | |
it("should do the right thing after the callback is invoked", function () { | |
// invoke the callback so we can verify behavior that happens after the callback is invoked | |
var args = TemplateResource.show.mostRecentCall.args, | |
callback = args[1]; | |
callback(); | |
// other assertions that happen after the callback | |
expect(instance.someSideEffectOfTheCallback).toBeTruthy(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment