Reece
Reece

Reputation: 21

Node sinon stub a chained function

So i have a piece of code:

General.helpers.elementContains = function(selector, value) {
return driver.findElement(webdriver.By.css(selector)).getInnerHtml().then(function(contents) {
  assert.equal(contents, value);
});

};

I would like to stub out the getInnerHtml function. I have currently stubbed out both the driver.findElement and the webdriver.By.css functions. My driver.findElement function returns a promise which i use the node module sinon-stub-promise.

sinon.stub(driver, 'findElement').returnsPromise();
sinon.stub(webdriver.By, 'css');

However when running the test as i am unsure of how to stub the .getInnerHtml function i get an error:

driver.findElement(...).getInnerHtml is not a function

I have tried changing the driver.findElement to return a getInnerHtml method which is stubbed and returns a value but i cannot seem to crack this one.

sinon.stub(driver.findElement).returns({getInnerHtml: sinon.stub().returns(value)})

Any help would be appreciated.

Upvotes: 1

Views: 761

Answers (1)

Reece
Reece

Reputation: 21

I have figured out a solution to this question:

Stub promise

promise = sinon.stub().returnsPromise();
sinon.stub(driver, 'findElement').returns({getInnerHtml: promise});

This works for me as getInnerHtml returns a promise and driver.findElement returns a object with getInnerHtml in which is a function which returns a promise.

Upvotes: 1

Related Questions