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
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
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")
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")