目前,我正在使用PhantomJS在我们的构建服务器上的QUnit和Sinon框架中运行Javascript单元测试.
但是,PhantomJS使用JavaScriptCore和JIT编译器作为其Javascript引擎.相反,我想使用在谷歌浏览器中使用的V8引擎,或在IE中使用的Chakra.我想这样做是因为我想检查代码的平台兼容性.
是否有像PhantomJS这样的流行测试跑步者使用这些引擎?
我试图使用sinon.js对Web套接字应用程序进行单元测试,
在sinon的 github上的一个用户做了这个,但我无法理解它如何帮助单元测试websocket应用程序来验证发送到假服务器的接收数据.
var dummySocket = { send : sinon.spy()};
sinon.stub(window, 'WebSocket').returns(dummySocket);
dummySocket = new WebSocket('ws://html5rocks.websocket.org/echo');
dummySocket.onopen();
dummySocket.onmessage(JSON.stringify({ hello : 'from server' }));
// You can assert whether your code sent something to the server like this:
sinon.assert.calledWith(dummySocket.send, '{"the client":"says hi"}');
Run Code Online (Sandbox Code Playgroud)
我的问题是
send假套接字对象的方法将数据发送到假服务器(例如: - socket.send())?dummySocket.onmessage = function (msg){}使用sinon.js,我无法获得任何进程来创建假的websocket对象,如假XMLHttpRequest和server分别使用useFakeXMLHttpRequest()和fakeServer.create()
在sinon.js上有没有实现这个的过程?
在使用moment函数调用它时,我无法存根构造format函数以返回预定义的字符串,这是我想要运行的示例规范mocha:
it('should stub moment', sinon.test(function() {
console.log('Real call:', moment());
const formatForTheStub = 'DD-MM-YYYY [at] HH:mm';
const momentStub = sinon.stub(moment(),'format')
.withArgs(formatForTheStub)
.returns('FOOBARBAZ');
const dateValueAsString = '2025-06-01T00:00:00Z';
const output = moment(dateValueAsString).format(formatForTheStub);
console.log('Stub output:',output);
expect(output).to.equal('FOOBARBAZ');
}));
Run Code Online (Sandbox Code Playgroud)
我能够看到这个输出使用console.log:
Real call: "1970-01-01T00:00:00.000Z"
Stub output: 01-06-2025 at 01:00
Run Code Online (Sandbox Code Playgroud)
但是测试失败导致01-06-2025 at 01:00 !== 'FOOBARBAZ'
我怎样才能正确存根moment(something).format(...)?
我正在尝试测试我的react组件中的一个方法.按下按钮后调用它,所以我用酶进行了模拟
it('clone should call handleCloneClick when clicked', () => {
const cloneButton = wrapper.find('#clone-btn');
cloneButton.simulate('click');
});
Run Code Online (Sandbox Code Playgroud)
我的组件方法在这里:
_handleCloneClick(event) {
event.preventDefault();
event.stopPropagation();
this.props.handleClone(this.props.user.id);
}
Run Code Online (Sandbox Code Playgroud)
当用户点击模拟中的按钮时,会调用_handleCloneClick,如何测试成功调用它?
我有一秒钟的jQuery .animate动作,在页面加载后5秒启动.我在我的Jasmine单元测试代码中设置了一个Sinon计时器,并在7秒钟后测试,以查看动画后属性是否应该如此.
它不能正常工作,所以我在我的Jasmine HTML测试页面上放置了一个动画实例,以便更好地了解正在发生的事情.
在Firefox和Chrome中,页面加载,动画函数被调用,单元测试立即失败,然后(也立即)动画明显发生.
在IE,Opera和Safari中,页面加载,动画函数被调用,单元测试立即失败,动画永远不会明显发生.
我希望的是以下(在所有浏览器中):
综观兴农的文档,这是假的定时器包括以下过程:
setTimeout,clearTimeout,setInterval,clearInterval,Date
我不知道jQuery的动画是如何工作的,但是我想它是使用CSS来转换,而且Sinon的CSS转换并没有涵盖useFakeTimers,所以我想这就是问题所在.但是,如果我对这个问题是正确的,我仍然需要一个解决方案.
也许我应该尝试除了诗乃之外的东西?Jasmine waits()在这个测试中的表现非常出色,但对于像我这样的不耐烦的人来说却是非常不切实际的.
还有其他建议吗?请记住,我是JS单元测试的新手,所以模糊的答案会让我更加困惑,而不是帮助我.; O)
如果我已经创建了一个实例 var a = sinon.createStubInstance(MyContructor).
我怎样才能替换其中一个存根函数var stub = sinon.stub(object, "method", func);.
我这样做的主要原因是希望实现多个回调解决方法,如上所述
我正在尝试使用上面列出的四个库来模拟我正在使用的组件的窗口对象.
我知道可以使用JSDom完成,但客户端反对使用它.基于我的研究,简单地做sinon.stub(窗口,'位置')应该工作但是当我运行我的测试时,我仍然在我的组件中未定义Window.
目前,在render return {window.location.host}中调用该组件
任何想法,我做错了让sinon剔除那一件.一旦我删除了那一块,那么我就可以专注于测试那个与窗口无关的组件的其他部分.
我的测试方法:
import React from 'react';
import { shallow } from 'enzyme';
import chai from 'chai';
chai.should();
import sinon from 'sinon';
import BillingStatementRow from '../BillingStatementRow';
describe('Test <BillingStatementRow /> Component', function() {
context('Function Testing', function() {
it('Test - onFieldChange - Make sure it handles NaN', function() {
var e = {target: {value: NaN}};
var window = { location : { host : "..." } };
var mockedOnChange = sinon.spy();
const wrapper = shallow (
<BillingStatementRow slds={''} …Run Code Online (Sandbox Code Playgroud) 我的渲染功能中有一个FileInput
<FileInput
accept= "image/jpeg,image/png,audio/mp3"
onChange= {this.fileInputOnChange}
children= {<i className="fa fa-paperclip"/>}
className= 'fileInput'
/>
Run Code Online (Sandbox Code Playgroud)
我需要编写一个文件上传测试,当我模拟更改函数时,它调用函数fileInputOnChange
fileInputOnChange: function(evt){
var file = evt.target.files[0];
var fileReader = new FileReader();
fileReader.onload = function(readFile){
// Check the file type.
var fileType = file.type.toLowerCase();
if(fileType.lastIndexOf('image') === 0 && (fileType.lastIndexOf('png') >= 0 || fileType.lastIndexOf('jpeg'))){
var image = new Image();
image.onload = function(){
var attachedFile = {
attached: file,
mediaSource: readFile.target.result,
type: 'image'
}
this.props.onChange(attachedFile);
}.bind(this);
image.onerror = function(){
this.props.onError("INVALID_TYPE");
}.bind(this);
image.src = readFile.target.result;
}else if(fileType.lastIndexOf('audio') === 0 && …Run Code Online (Sandbox Code Playgroud) 我正在开发一个包装器组件,用于在React中平滑地加载图像.我使用含有mocha,chai和sinon的酶对我的组件进行单元测试.在这里的测试中,我试图测试:
加载图像时更新组件的状态
onLoad调用组件上的实例方法
const wrapper = shallow( );
const onLoad = wrapper.find('img').props().onLoad;
const onLoadSpy = sinon.spy(onLoad); wrapper.update();
const status = wrapper.state().status;
expect(onLoadSpy).to.have.been.called;
expect(status).to.equal('LOADED');
我发现状态更新既不会被酶反映,也不会更新onLoad间谍的通话计数.这是测试的相应代码:
export default class Image extends Component {
constructor(props) {
super(props);
if (props.src != null && typeof props.src === 'string') {
this.state = {
status: LOADING,
};
} else {
this.state = {
status: PENDING,
};
}
this.onLoad = this.onLoad.bind(this);
}
onLoad() {
this.setState({
status: LOADED,
});
}
render() {
//lots of code above …Run Code Online (Sandbox Code Playgroud) 让我说我有一个功能
Func a() {
//Do Something
let c = b();
return c;
}
Run Code Online (Sandbox Code Playgroud)
我想测试函数a和mock b(),并在mock中想要分配c.Sinon.Stub(试验中, "b")返回( "DummyValue"); c应该分配DummyValue.
我怎样才能做到这一点?
describe("a", () => {
let a = a();
//mock b();
action = execute(a);
expect(action).should.return.("DummyValue");
})
Run Code Online (Sandbox Code Playgroud)