2/2/2024 0 Comments Mostrecent args jasminespyOn(thing, 'addThing').and.callThrough() Įxpect(thing.addThing).toHaveBeenCalledTimes(2) īecause the spy is still and.callThrough, thing1 and thing2 will be added to stuff. To test that a function has been called the appropriate amount of times, use the toHaveBeenCalledTimes matcher. The function will still do what it was going to do, so if we displayed the contents of stuff it would be. When thing.addThing is called, the spy will register that it was called and this can now be used by the expect. thing.addThing('thing1') Įxpect(thing.addThing).toHaveBeenCalled() The following expect just tests that our spy has been called, not caring about parameters, how many times it may have been called, or anything else. If all we want to know is that our function that is being spied on has been called, toHaveBeenCalled is what we are looking for. Now that the and.callThrough spy is installed, let’s look at what we can do with it by looking at some expect statements. For the spy on addThing we are saying that we want to install a spy on thing and that we are replacing the method with the method name addThing. spyOn(thing, 'addThing').and.callThrough() įor all the spies in this article, we will be using spyOn(obj, methodName) to install our spies. Now let’s install an and.callThrough spy for the addThing function of thing. This means that when we use this spy, the function being spied on will still do what it is written to do, but we can track the calls to it. This spy will track calls to the function it is spying on, but it will also pass through to the function and allow it to run. The first spy we will look at is the and.callThrough spy. Let’s take a look at the Jasmine Spies I use most often when writing unit tests. For this reason, when working on these tests, we will be using describe blocks per function that is being tested. Jasmine Spies are a powerful tool to use in unit testing because they allow us to focus on the function that is being tested.Ī spy will only exist in the describe or it block that it is defined. A Jasmine spy can stub any function and track all calls to that function and all of its arguments.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |