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 8863217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:55:19+00:00 2026-06-14T15:55:19+00:00

I have a simple test suite that has one it function inside of it.

  • 0

I have a simple test suite that has one it function inside of it. I want to see if a certain function is called within the function I’m calling, so I have something like this:

describe("doStuff", function () {
    var foo = new Foo();
    spyOn(foo, "doOtherStuff");
    foo.doStuff(true);
    it("should do stuff and other stuff", function() {
        expect(foo.stuffDone).toBe(true);
        expect(foo.doOtherStuff).toHaveBeenCalled();
    });
});

However, this gives me the error: Expected a spy, but got Function.

After looking around some, I saw all examples had the spyOn in a beforeEach. So, I changed my test to:

describe("doStuff", function () {
    var foo = new Foo();
    beforeEach(function() {
        spyOn(foo, "doOtherStuff");
        foo.doStuff(true);
    });
    it("should do stuff and other stuff", function() {
        expect(foo.stuffDone).toBe(true);
        expect(foo.doOtherStuff).toHaveBeenCalled();
    });
});

And this works. I’m pretty new to jasmine, so I may just be missing something obvious, but I just want to know why it has to be in a beforeEach for the spyOn to work. It’s easy enough to just use the beforeEach, but I want to understand better what is going on. Thanks.

  • 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-06-14T15:55:20+00:00Added an answer on June 14, 2026 at 3:55 pm

    That is simply because Jasmine runs the Specs in a different closure. The describe and it calls only register callbacks that are added to a queue and then executed by Jasmine later. And Jasmine always cleans up the spies …

    But you can also add the spyOn to the it callback.

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

Sidebar

Related Questions

I have a simple test application (C# console application) that does an HTTP GET
I have what is a simple problem that hopefully has a simple solution: I
I have a simple Rails 3.1.rc6 app that I'm using to try to test
I have a super simple map reduce test... that isn't working consistently. In a
I have a simple test case failing in Django: Model (app/models.py): from django.db import
I have this simple test project just to test the IncludeExceptionDetailInFaults behavior. public class
I have created a simple test form with FormBorderStyle = FixedToolWindow by default and
I have a simple unit test case (extensive question here ) on a configuration
I have made a simple test application for the issue, two winforms each containing
I have a very simple test page for PHP session. <?php session_start(); if (isset($_SESSIONS['views']))

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.