title: Unit testing with Jasmine: code in beforeEach() not seen by test's spyOn()
body: <p>New to unit testing in general and Jasmine in particular.</p>
  
  <p>I've set a variable in a <code>beforeEach()</code> callback, but it doesn't seem to work on the second test. It's supposed to fire initialization stuff in advance of <em>every</em> test within its context, right? I'm sure my <code>spyOn()</code> call is to blame, but I don't know how to fix it.</p>
  
  <p>Comments explain the passes and fails:</p>
  
  <pre><code>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");
        });
  
      });
  
    });
  });
  </code></pre>
  
  <p>The <a href="https://github.com/pivotal/jasmine/wiki/Spies" rel="nofollow">documentation</a> explains the usage, but not the context, of <code>spyOn()</code>, so I can't tell if I've created a bug or if I'm unknowingly taking advantage of a feature.</p>
  
  <p>I can post the constructor if anyone thinks it makes any difference in the diagnosis, but I can assure you it's dead simple.</p>
  
  <p>I'm sure it's a straightforward fix using some basic unit testing concept I'm having to learn the hard way. Thanks in advance.</p>
  
  <p><strong>P.S.</strong> <em>I realize what I'm testing for in that failing spec isn't what I've described. I'm working my way through the API guide, looking for a way to get to the arguments array within a function call, so I can do a specific test on <code>arguments[0]</code>. Hints are appreciated, but not necessary. I'll figure it out.</em></p>