Unit testing with Jasmine: code in beforeEach () not seen by test spyOn ()

New for unit testing in general and Jasmine in particular.

I set the variable in the beforeEach() , but it doesn't seem to work in the second test. It should run the initialization material in advance of each test in its context, right? I'm sure my spyOn() call is to blame, but I don't know how to fix it.

Comments explain passages and fail:

 describe("Test suite for my library", function () { var html, body, play, ... // custom matcher... beforeEach(function () { this.addMatchers({ toBeInstanceOf : function (constructr) { return this.actual instanceof constructr; }); }); }); describe("Within the Button object", function () { beforeEach(function () { play = new Button("play", false); }); describe("play", function () { // This test passes, as expected... it("should be an instance of the Button object", function () { expect(play).toBeInstanceOf(Button); }); }); describe("play.name", function () { // This test failed with the message // "Expected spy Button to have been called // with [ 'play', false ] but it was never called." it("should be the first argument passed to the Button constructor", function () { spyOn(window, "Button"); play = new Button("play", false); // ...until I added this line. Now it passes. expect(window.Button).toHaveBeenCalledWith("play", false); }); // This test passes, even if the one above fails. it("should be 'play'", function () { expect(play.name).toBe("play"); }); }); }); }); 

The documentation explains the use, but not the context, of spyOn() , so I cannot say if I created an error or if I unknowingly used a function.

I can publish the constructor if someone thinks that it has some meaning in the diagnosis, but I can assure you that it is simply dead.

I'm sure this is a simple fix that uses the concept of basic unit testing, which I need to learn. Thanks in advance.

PS I understand that I am testing in this unsuccessful specification, this is not what I described. I am working my way through the API manual, looking for a way to get to the array of arguments in a function call, so I can run a specific test on arguments[0] . Tips are appreciated but not needed. I will find out.

+4
source share
2 answers

Short answer: No, before each and the spies are incompatible

You must make a spy before you call if you want the spy to know about the call. You can use spyOn (object, 'function') and CallThrough () if you do not want to interfere with its default behavior.

Long answer: the method of falsification / bullying / stubbing / spying frameworks often works by replacing the method you call with a method that the mixing system can control. Any calls to this function before it is replaced by a spy cannot be honored. Itโ€™s good, although a little uncomfortable,

+6
source

Its the reason you look in the .Button window after you call. I'm not quite sure what the spy does, but in the end he shifted the function you are spying with to another function, where he can verify that the function was called and which arguments were passed. When you create your button before starting the test, the original window.button function is window.button . Then you replace the function with a spy and verify that the spy is called, so your test should fail.

It seems either create your button in the test itself, or create your spy before you call a new button in your beforeEach function.

+4
source

Source: https://habr.com/ru/post/1390508/


All Articles