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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:14:13+00:00 2026-05-22T20:14:13+00:00

I am unit testing some JavaScript with Jasmine and wish to spy on (mock)

  • 0

I am unit testing some JavaScript with Jasmine and wish to spy on (mock) an element of the DOM that is accessed by a jQuery selector.

My spec is:

it("should be able to mock DOM call", function() {

    spyOn($("#Something"), 'val').andReturn("bar");

    result = $("#Something").val();

    expect(result).toEqual("bar");

});

In my specrunner.html I have:

<input type="hidden" id="Something" value="foo" />

Unfortunately the spec fails with:

should be able to mock DOM call Expected ‘foo’ to equal ‘bar’.

  • 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-22T20:14:14+00:00Added an answer on May 22, 2026 at 8:14 pm

    This line is wrong:

    spyOn($("#Something"), 'val').andReturn("bar");
    

    Jasmine’s spyOn function expects two parameters. The first is an existing object. The second is a function name as a string. You are correctly passing in the function name as a string (“val”) but you are not passing in an existing object as the first parameter.

    $("#Something")
    

    …is not an existing object. It is the result (the return value) of a jQuery selector. More specifically, it will return a jQuery object representing the matched nodes – kind of like an array of results.

    $
    

    …is an existing object.

    $.fn
    

    …is an existing object.

    $("#Something")
    

    …is not an existing object – it is the result of a jQuery selector.

    This will work:

    it("should be able to mock DOM call", function () {
        //spyOn($.fn, "val").andReturn("bar"); //pre-jasmine 2.0 syntax
        spyOn($.fn, "val").and.returnValue("bar"); //Jasmine 2.0 Syntax
        var result = $("#Something").val();
        expect(result).toEqual("bar");
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to incorporate some JavaScript unit testing into my automated build process. Currently
I'm having trouble unit testing a method that changes some properties of a reference
I've been trying to implement unit testing and currently have some code that does
I am doing some unit testing for a DAO that works with postgresql. Some
We are using Rhino Mocks to perform some unit testing and need to mock
I'm using the JavaScript support in javax.script to do some basic unit testing of
I was doing some unit testing on code that could throw a number of
I've recently come across some unit testing code that imports the modules to test
I want to introduce Unit Testing to some colleagues that have no or little
So I'm interested in doing some unit testing of a library that interacts with

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.