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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:25:10+00:00 2026-05-28T08:25:10+00:00

How do I mock window object? I’m doing firefox extension and I want to

  • 0

How do I mock window object? I’m doing firefox extension and I want to use jasmine for javascript testing.

In my javascript I have


function submit() {
...
var url = window.arguments[0];
...
}

Obviously, I have to mock window.arguments[0] in jasmine because that object doesn’t exists if not passing any parameter from window.openDialog

This is my attempt to mock it with “with”


it("should submit to server", function() {

        var localContext = {
            "window": {
                arguments: ["http://localhost"]
            }

        }

        with(localContext);

But I still get this error TypeError: Cannot read property ‘0’ of undefined, it’s like when the test is run window.arguments[0] gets wiped out with the real window, because if I do


window.arguments[0]

inside the test, it prints out “http://localhost” correctly. but when it comes to submit() method it shows the error that window.argument is undefined.

  • 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-28T08:25:11+00:00Added an answer on May 28, 2026 at 8:25 am

    The problem is that you just overwrite a property of the window object. And if you can do that, the browser can do that as well. So mocking a function or property of a global object that everyone can access isn’t a good idea in general, because you can never be sure that your changes will be there when you try to access them.

    Which brings me to dependency injection. Its a common pattern to make your code unit testable, with a focus on unit. Whats does it mean. When ever you create a new object or access a global object, you’re not only testing your unit functionality, but also the functionality of your newly created or global object. To prepend that, you not create the new objects in your unit, but pass them into. Normally you would to this in the constructor, but as in JavaScript function are objects with the function body as constructor, you can also pass the dependencies simply into your function.

    So in your case, the function depends on the global window object. So instead of trying to access the global window object, pass it as a parameter into your function. Doing it this way you can pass in the window object in your production code and a simple JavaScript object with an arguments atribute into your test:

    function youWannaTest(w){
        console.log(w.arguments[0]);
    }
    

    In your extension call the function like this:

    youWannaTest(window);
    

    In your test call the function like this:

    youWannaTest({arguments: ['someValue']});
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many people use Mock Objects when they are writing unit tests. What is a
I am using a mock object in RhinoMocks to represent a class that makes
I'm new to mock objects, but I understand that I need to have my
I'm using the mock library written by Michael Foord to help with my testing
How can I mock a DataServiceQuery for unit testing purpose? Long Details follow: Imagine
var mockedService = new Mock<IService>(); mockedService.Setup(x => x.InterfaceMethod(args)).Returns(value); _Service = mockedService.Object; MyController controller =new
Here's the mock-up a client asked me to create: Here's what I have in
I want to mock the User property of an HttpContext. I'm using Scott Hanselmans
I'm creating a mock data source that I want to be able to pass
How do I mock an object with a constructor using Rhino Mocks? For example

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.