Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7049515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:01:36+00:00 2026-05-28T03:01:36+00:00

New to unit testing in general and Jasmine in particular. I’ve set a variable

  • 0

New to unit testing in general and Jasmine in particular.

I’ve set a variable in a beforeEach() callback, but it doesn’t seem to work on the second test. It’s supposed to fire initialization stuff in advance of every test within its context, right? I’m sure my spyOn() call is to blame, but I don’t know how to fix it.

Comments explain the passes and fails:

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 usage, but not the context, of spyOn(), so I can’t tell if I’ve created a bug or if I’m unknowingly taking advantage of a feature.

I can post the constructor if anyone thinks it makes any difference in the diagnosis, but I can assure you it’s dead simple.

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.S. 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 arguments[0]. Hints are appreciated, but not necessary. I’ll figure it out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:01:37+00:00Added an answer on May 28, 2026 at 3:01 am

    Short answer: No, Before each and spies are not incompatible

    You must Spy before you call if you want the spy to know about the call. You can use spyOn(object, ‘function’).andCallThrough() if you do not wish to interfere with its default behavior.

    Long answer: The way faking/mocking/stubbing/spying frameworks often work is by replacing the method you are calling with a method that the mocking framework can control. Any calls to that function before it is replaced with the spy cannot be observed. This is a good thing, though mildly inconvenient,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to unit testing in general, but wanted to implement it in MVC
I'm new to unit testing using nunit (and Java development in general). When creating
I'm new to unit testing. But how do I unit test my DAL which
I'm new to unit testing and NUit in particular. I'm just typing some examples
I'm just getting stated with boost-test and unit testing in general with a new
I am quite new to unit testing and testing in general. I am developing
I'm a relatively new convert to unit testing in general and I've run into
I am new to rhino mocks and unit testing in general. I am starting
I am new to unit testing, so I am probably misunderstanding something big, but
I'm fairly new to unit testing but I'm trying to incorporate it into my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.