一些背景知识:我想为以下功能编写单元测试。但是,每当我将该功能导入单元测试并尝试运行它时,我都会收到ReferenceError: $ is not defined
。 $
来自量角器库,因此我一直在寻找一种对它进行存根的方法,但我一直无法找到一种方法来做到这一点。到目前为止,我已经尝试使用sinon和嘲笑库。
我的问题是我该如何模拟$
以便为此编写单元测试?
const { $ } = require('protractor');
/**
* Function for resolving object types.
*
*
* resolveType($('.save-button'));
* ```
*
* @param {*} locator
* @return {Object} returns object with the element, a selector, and
* it's description for element
*/
module.exports = function verifyType(locator) {
return {
element: $(mySelector),
selector: mySelector,
description: locator.description,
};
};
更新-这是我目前为此所做的单元测试尝试。它以ReferenceError: $ is not defined
失败:
const resolveType = require('../../path/toResolveType').resolveType;
const mochaccino = require('mochaccino');
const { expect } = mochaccino;
const sinon = require('sinon');
const { $ } = require('protractor');
const locator_a = { description: '"embed ID" text box', selector: '#embed-id' };
describe('commands.resolveType', function() {
beforeEach(function () {
sinon.stub($).returns('test');
});
afterEach(function () {
$.restore();
});
it('Standard locator with CSS selector', function() {
const answer = resolveType(locator_a);
// Placeholder till I can get answer to evaluate.
expect('a').toEqual('a');
});
});