我正在使用Mocha,Chai,Karma,Sinon,Webpack进行单元测试.
我按照此链接配置了React-Redux Code的测试环境.
我可以成功测试我的动作和reducers javascript代码,但是当涉及到测试我的组件时,它总是会产生一些错误.
import React from 'react';
import TestUtils from 'react/lib/ReactTestUtils'; //I like using the Test Utils, but you can just use the DOM API instead.
import chai from 'chai';
// import sinon from 'sinon';
import spies from 'chai-spies';
chai.use(spies);
let should = chai.should()
, expect = chai.expect;
import { PhoneVerification } from '../PhoneVerification';
let fakeStore = {
'isFetching': false,
'usernameSettings': {
'errors': {},
'username': 'sahil',
'isEditable': false
},
'emailSettings': {
'email': 'test@test.com',
'isEmailVerified': false,
'isEditable': false …Run Code Online (Sandbox Code Playgroud) 我正在使用Node.jsfelixge的node-mysql客户端.我没有使用ORM.
我正在测试Vows并希望能够模拟我的数据库,可能使用Sinon.由于我本身并没有真正的DAL(除了node-mysql),我不确定如何解决这个问题.我的模型大多是简单的CRUD,有很多吸气剂.
有关如何实现这一目标的任何想法?
我正在试着弄清楚如何使用sinon模拟一个构造函数.我有一个函数,通过调用接受一些参数的构造函数来创建多个小部件.我想验证构造函数是否使用正确的参数调用了正确的次数,但我不想实际构造小部件.以下链接似乎解释了一种模拟构造函数的直接方法,但它对我不起作用:
http://tinnedfruit.com/2011/03/25/testing-backbone-apps-with-jasmine-sinon-2.html
当我对存根构造函数进行以下调用时:
sinon.stub(window, "MyWidget");
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Uncaught TypeError: Attempted to wrap undefined property MyWidget as function
Run Code Online (Sandbox Code Playgroud)
在Chrome中进行调试时,我看到MyWidget显示在Scope Variables的Local部分中,但是窗口外没有MyWidget属性.
任何帮助将不胜感激.
我想验证各种日期字段是否已正确更新,但我不想乱用预测何时new Date()被调用.如何删除Date构造函数?
import sinon = require('sinon');
import should = require('should');
describe('tests', () => {
var sandbox;
var now = new Date();
beforeEach(() => {
sandbox = sinon.sandbox.create();
});
afterEach(() => {
sandbox.restore();
});
var now = new Date();
it('sets create_date', done => {
sandbox.stub(Date).returns(now); // does not work
Widget.create((err, widget) => {
should.not.exist(err);
should.exist(widget);
widget.create_date.should.eql(now);
done();
});
});
});
Run Code Online (Sandbox Code Playgroud)
如果它是相关的,这些测试在节点应用程序中运行,我们使用TypeScript.
如何在每次测试前重置Sinon间谍的"被叫"计数?
这就是我现在正在做的事情:
beforeEach(function() {
this.spied = sinon.spy(Obj.prototype, 'spiedMethod');
});
afterEach(function() {
Obj.prototype.spiedMethod.restore();
this.spied.reset();
});
Run Code Online (Sandbox Code Playgroud)
但是当我在测试中检查呼叫计数时:
it('calls the method once', function() {
$.publish('event:trigger');
expect(this.spied).to.have.been.calledOnce;
});
Run Code Online (Sandbox Code Playgroud)
...测试失败并报告该方法被调用X次(每次上一次测试也触发同一事件一次).
我真的不知道如何在react组件的子代中模拟内联函数
我栈:sinon,chai,enzyme,
组件用法:
<ListItem onClick={() => someFn()} />
Run Code Online (Sandbox Code Playgroud)
组件的渲染:
render() {
return (
<li>
<a href="#" onClick={e => {
e.preventDefault();
this.props.onClick();
}}
> whatever </a>
</li>
);
}
Run Code Online (Sandbox Code Playgroud)
我们在这里onClick打电话e.preventDefault().如何告诉<a href>(link)不要打电话e.preventDefault()?我怎么能嘲笑那个onClick?
以下我在测试中尝试:
浅拷贝设置
function setup() {
const someFn = sinon.stub();
const component = shallow(
<ListItem
onClick={() => {
someFn();
}}
/>
);
return {
component: component,
actions: someFn,
link: component.find('a'),
listItem: component.find('li'),
} …Run Code Online (Sandbox Code Playgroud) 所以我刚刚开始使用sinon.js&为我正在进行的javascript应用程序编写测试jasmine.js.总体上工作得很好,但我还需要能够测试我的路由器.
处于当前状态的路由器将触发许多视图和其他内容,jasmine.js通过调用Backbone.navigate依赖于应用程序状态和UI itneraction来终止当前测试.
那么我怎样才能测试到不同位置的路由是否可行,同时保持路由器"沙盒化"并且不允许它们改变路由?
我可以设置一些监视pushState更改或类似的模拟函数吗?
有什么区别
stub.yield([arg1, arg2, ...])spy.yields([arg1, arg2, ...])stub.callsArg(index)在Sinon.js存根库中?
stub.yield() 是我能够掌握的唯一一个:
stub = sinon.stub(API, 'call_remote');
callback = sinon.spy();
API.call_remote('help', callback);
@stub.yield( "solution!" );
@stub.calledOnce.should.be.true;
@callback.calledOnce.should.be.true;
@callback.args[0][0].should.eql( "solution!" );
Run Code Online (Sandbox Code Playgroud)
在使用should.js进行测试时,所有断言都会通过.
是否有类似的测试模式stub.yields()和stub.callsArg(index)?
文档没有提供任何示例来澄清这两种方法,但我对它们很好奇.
考虑以下示例Javascript代码:
function privateFunction (time) {
if (time < 12) { console.log('Good morning'); }
if (time >= 12 && time <19) { console.log('Good afternoon'); }
else { console.log('Good night!'); }
};
Run Code Online (Sandbox Code Playgroud)
我应该如何使用mocha(也可能是sinonjs)对nodejs进行单元测试,注意到这是一个在模块内部调用的私有函数?我需要传入参数并检查函数是否正在将正确的东西记录到控制台.
我可以用console.warn和console.error吗?
我在Express中有以下内容
//index.js
var service = require('./subscription.service');
var auth = require('../auth/auth.service');
var router = express.Router();
router.post('/sync', auth.isAuthenticated, service.synchronise);
module.exports = router;
Run Code Online (Sandbox Code Playgroud)
我想覆盖或模拟isAuthenticated返回此
auth.isAuthenticated = function(req, res, next) {
return next();
}
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试:
it('it should return a 200 response', function(done) {
//proxyquire here?
request(app).post('/subscriptions/sync')
.set('Authorization','Bearer '+ authToken)
.send({receipt: newSubscriptionReceipt })
.expect(200,done);
});
Run Code Online (Sandbox Code Playgroud)
我尝试使用proxyquire模拟index.js - 我想我需要存根路由器?我也试过在测试中覆盖
app.use('/subscriptions', require('./api/subscription'));
Run Code Online (Sandbox Code Playgroud)
必须有一种简单的方法来模拟它,所以我不需要验证请求.有任何想法吗?
sinon ×10
javascript ×5
mocha.js ×5
unit-testing ×5
node.js ×3
mocking ×2
reactjs ×2
backbone.js ×1
chai ×1
constructor ×1
enzyme ×1
express ×1
jasmine ×1
mysql ×1
proxyquire ×1
redux ×1
stub ×1
testing ×1
vows ×1