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.