# Spies, Stubs & Clocks

Examples of using stubs, spies, and controlling clock time - for a full reference of commands, go to docs.cypress.io

#### cy.spy()

To wrap a method in a spy, use the `cy.spy()` command.

    var obj = {
      foo () {}
    }

    var spy = cy.spy(obj, "foo").as("anyArgs")

    obj.foo()

    expect(spy).to.be.called

#### cy.stub()

To create a stub and/or replace a function with a stub, use the `cy.stub()` command.

    var obj = {
      foo () {}
    }

    var stub = cy.stub(obj, "foo").as("foo")

    obj.foo("foo", "bar")

    expect(stub).to.be.called

#### cy.clock()

To control time in the browser, use the `cy.clock()` command.

    var now = new Date(2017, 2, 14).getTime() // March 14, 2017 timestamp

    cy
      .clock(now)
      .visit('http://localhost:8080/commands/spies-stubs-clocks')
      .get("#clock-div").click()
        .contains("2017-03-14")

Click for current time!

#### cy.tick()

To move time in the browser, use the `cy.tick()` command.

    var now = new Date(2017, 2, 14).getTime() // March 14, 2017 timestamp

    cy
      .clock(now)
      .visit('http://localhost:8080/commands/spies-stubs-clocks')
      .get("#clock-div").click()
        .contains("2017-03-14T04:00:00.000Z")
      .tick(10000) // 10 seconds passed
      .get("#clock-div").click()
        .contains("2017-03-14T04:00:10.000Z")

Click for current time!