我在AngularJS 1.X项目上使用Typescript.我使用不同的Javascript库用于不同的目的.要对我的源进行单元测试,我想使用Typings(= interfaces)来存根一些依赖项.我不想使用ANY类型,也不想为每个接口方法编写一个空方法.
我正在寻找一种方法来做这样的事情:
let dependency = stub(IDependency);
stub(dependency.b(), () => {console.log("Hello World")});
dependency.a(); // --> Compile, do nothing, no exception
dependency.b(); // --> Compile, print "Hello World", no exception
Run Code Online (Sandbox Code Playgroud)
我现在的痛苦是,我要么使用any和实现在我的测试用例中调用的所有方法,要么实现接口并实现完整的接口.这是太多无用的代码:(.
如何为每个方法生成一个具有空实现的对象并键入?我使用Sinon进行嘲弄,但我也可以使用其他库.
PS:我知道Typescript会删除接口......但我仍然想解决这个问题:).
我想对下面的ES6类进行单元测试:
// service.js
const InternalService = require('internal-service');
class Service {
constructor(args) {
this.internalService = new InternalService(args);
}
getData(args) {
let events = this.internalService.getEvents(args);
let data = getDataFromEvents(events);
return data;
}
}
function getDataFromEvents(events) {...}
module.exports = Service;
Run Code Online (Sandbox Code Playgroud)
我如何嘲笑与Sinon.JS构造,以模拟getEvents的internalService测试getData?
我查看了使用Sinon的Javascript:Mocking Constructor,但无法提取解决方案.
// test.js
const chai = require('chai');
const sinon = require('sinon');
const should = chai.should();
let Service = require('service');
describe('Service', function() {
it('getData', function() {
// throws: TypeError: Attempted to wrap undefined property …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试一些客户端代码,为此我需要window.location.href使用Mocha/Sinon 来存储属性的值.
到目前为止我尝试过的(使用此示例):
describe('Logger', () => {
it('should compose a Log', () => {
var stub = sinon.stub(window.location, 'href', 'http://www.foo.com');
});
});
Run Code Online (Sandbox Code Playgroud)
运行器显示以下错误:
TypeError:自定义存根应该是函数或属性描述符
将测试代码更改为:
describe('Logger', () => {
it('should compose a Log', () => {
var stub = sinon.stub(window.location, 'href', {
value: 'foo'
});
});
});
Run Code Online (Sandbox Code Playgroud)
这产生了这个错误:
TypeError:尝试将字符串属性href包装为函数
将函数作为第三个参数传递sinon.stub也不起作用.
有没有办法提供假window.location.href字符串,也避免重定向(因为我在浏览器中测试)?
我有一个包含类似语句成分this.$route.fullPath,我应该怎么嘲笑的值fullPath的$route对象,如果我想测试组件?
在以下示例中,我想存根该get函数以防止发生实际的HTTP请求.我想刺探的get方法来检查什么样的参数,它被称为用.
var request = require('request'), sinon = require('sinon');
describe('my-lib', function() {
sinon.stub(request, 'get').yield(null, null, "{}");
var spy = sinon.spy(request, 'get');
it('should GET some data', function(done) {
function_under_test(function(err, response) {
if(error) return done(error);
assert(request.get.called);
assert(request.get.calledWith('some', 'expected', 'args'));
});
});
});
Run Code Online (Sandbox Code Playgroud)
不过,诗乃似乎不允许间谍和抄袭相同的方法.上面的示例给出以下错误:
TypeError: Attempted to wrap get which is already wrapped
Run Code Online (Sandbox Code Playgroud)
我如何监视方法,同时防止默认行为?
我正在尝试将React组件方法存根用于测试目的:
var Comp = React.createClass({
displayName: "Comp",
plop: function() {
console.log("plop");
},
render: function() {
this.plop();
return React.DOM.div(null, "foo");
}
});
var stub = sinon.stub(Comp.type.prototype, "plop");
React.addons.TestUtils.renderIntoDocument(Comp());
sinon.assert.called(stub); // throws
Run Code Online (Sandbox Code Playgroud)
这令人遗憾地继续将"plop"打印到控制台上......并且断言失败了.
注意:直接存在spec对象方法是有效的,但是你必须分别导出组件构造函数和规范,这样它们在测试中都可用...而且,你甚至需要在创建组件类之前存根规范; 不太方便:
var CompSpec = {
displayName: "Comp",
plop: function() {
console.log("plop");
},
render: function() {
this.plop();
return React.DOM.div("foo");
}
};
var stub = sinon.stub(CompSpec, "plop");
var Comp = React.createClass(CompSpec);
React.addons.TestUtils.renderIntoDocument(Comp());
// plop() is properly stubbed, so you can
sinon.assert.called(stub); // pass
Run Code Online (Sandbox Code Playgroud)
您能想到另一种轻松存根React组件方法的策略吗?
如果我将sinon与typescript一起使用,那么如何将sinon mock转换为我的对象的实例?
例如,将返回SinonMock,但我的测试控制器可能需要传递给其构造函数的特定服务.
var myServiceMock: MyStuff.MyService = <MyStuff.MyService (sinon.mock(MyStuff.MyService));
controllerUnderTest = new MyStuff.MyController(myServiceMock, $log);
Run Code Online (Sandbox Code Playgroud)
sinon可以和Typescript一起使用吗?
我想验证从我的单元测试中bar()调用内部foo().
我认为Sinon间谍可能是合适的,但我不知道如何使用它们.
有没有办法检查方法是否被调用?甚至可能提取调用中使用的参数bar()?
var spy = sinon.spy(foo);
function foo(){
bar(1,2,3);
}
function bar(){ }
foo();
// what to do with the spy?
Run Code Online (Sandbox Code Playgroud)
有没有办法使用jest API存根函数?我习惯使用sinon存根,在那里我可以使用存根编写单元测试,用于从我测试的单元出来的任何函数调用 - http://sinonjs.org/releases/v1.17.7/stubs/
例如-
sinon.stub(jQuery, "ajax").yieldsTo("success", [1, 2, 3]);
Run Code Online (Sandbox Code Playgroud) sinon ×10
javascript ×6
unit-testing ×6
mocha.js ×2
mocking ×2
stubbing ×2
typescript ×2
angularjs ×1
jasmine ×1
jestjs ×1
node.js ×1
reactjs ×1
stub ×1
vue-loader ×1
vuejs2 ×1