Files
cypress/docs/source/api/commands/stub.md
T
2017-05-19 12:01:36 -04:00

4.6 KiB

title, comments, description
title comments description
stub true

A stub is used to replace a function, record its usage and control its behavior. You can track calls to the functions and what arguments the function was called with. You can also control what the function returns and even cause it to throw an exception.

cy.stub returns a sinon.js stub. All methods found on sinon.js spies and stubs are supported. cy.stub creates stubs in a sandbox, so all stubs created are automatically reset/restored between tests without you having to explicitly reset/restore them.

Cypress has built-in sinon-as-promised support, so the stubs returned by cy.stub supports the .resolves and .rejects API provided by sinon-as-promised.

Cypress also has built-in sinon-chai support, so any assertions supported by sinon-chai can be used without any configuration.

Unlike most Cypress commands, cy.stub is synchronous and returns a value (the stub) instead of a Promise-like chain-able object.

Returns the stub

cy.stub()

Creates and returns a stub. See the sinon.js stub docs for methods on the stub.

cy.stub( object, "method" )

Replaces the method on the object with a stub and returns the stub. See the sinon.js stub docs for methods on the stub.

cy.stub( object, "method", replacerFn )

Replaces the method on the object with the replacerFn wrapped in a spy.See the sinon.js spy docs for methods on the spy.

Usage

Create a stub and manually replace a function

// assume App.start calls util.addListeners
util.addListeners = cy.stub()
App.start()
expect(util.addListeners).to.be.called

Replace a method with a stub

// assume App.start calls util.addListeners
cy.stub(util, "addListeners")
App.start()
expect(util.addListeners).to.be.called

Replace a method with a function

// assume App.start calls util.addListeners
let listenersAdded = false
cy.stub(util, "addListeners", function () {
  listenersAdded = true
})
App.start()
expect(listenersAdded).to.be.true

Specify the return value of a stubbed method

// assume App.start calls util.addListeners, which returns a function
// that removes the listeners
const removeStub = cy.stub()
cy.stub(util, "addListeners").returns(removeStub)
App.start()
App.stop()
expect(removeStub).to.be.called

Example Recipe

{% note info Using cy.stub %} Check out our example recipe testing spying, stubbing and time {% endnote %}

Alias a stub

Adding an alias using cy.as to stubs makes them easier to identify in error messages and Cypress's command log.

const obj = {
  foo () {}
}
const stub = cy.stub(obj, "foo").as("anyArgs")
const withFoo = stub.withArgs("foo").as("withFoo")
obj.foo()
expect(stub).to.be.called
expect(withFoo).to.be.called // purposefully failing assertion

You will see the following in the command log:

stubs with aliases

Command Log

Create a stub, alias it, and call it

const obj = {
  foo () {}
}
const stub = cy.stub(obj, "foo").as("foo")
obj.foo("foo", "bar")
expect(stub).to.be.called

The command above will display in the command log as:

screen shot of command log

When clicking on the (stub-1) event within the command log, the console outputs the following:

screen shot of console output

Related