我有一个非常简单的 JS 库(称为 trysinon.js),如下所示:
export function foo() {
bar();
}
export function bar() {
return 2;
}
Run Code Online (Sandbox Code Playgroud)
我有以下测试
import expect from 'expect';
import sinon from 'sinon';
import * as trysinon from 'trysinon';
describe('trying sinon', function() {
beforeEach(function() {
sinon.stub(trysinon, 'bar');
});
afterEach(function() {
trysinon.bar.restore();
});
it('calls bar', function() {
trysinon.foo();
expect(trysinon.bar.called).toBe(true);
});
});
Run Code Online (Sandbox Code Playgroud)
并且测试失败了。如何确保测试通过?
这是我的代码:start_end.js
var glb_obj, test={};
var timer = 10;
test.start_pool = function(cb) {
timer--;
if(timer < 1) {
glb_obj = {"close": true}; // setting object
cb(null, "hello world");
} else {
start_pool(cb);
}
}
test.end_pool = function(){
if(glb_obj && glb_obj.close) {
console.log("closed");
}
}
module.exports = test;
Run Code Online (Sandbox Code Playgroud)
测试用例:
var sinon = require('sinon');
var start_end = require('./start_end');
describe("start_end", function(){
before(function () {
cb_spy = sinon.spy();
});
afterEach(function () {
cb_spy.reset();
});
it("start_pool()", function(done){
// how to make timer variable < 1, so …Run Code Online (Sandbox Code Playgroud) 我刚刚开始使用 sinon,并且在消除 DynamoDB 调用方面取得了一些初步成功:
sandbox = sinon.createSandbox()
update_stub = sandbox.stub(AWS.DynamoDB.DocumentClient.prototype, 'update').returns({
promise: () => Promise.resolve(update_meeting_result)
})
Run Code Online (Sandbox Code Playgroud)
这很好用。
但我还需要存根 Lambda,但同样的方法不起作用:
lambda_stub = sandbox.stub(AWS.Lambda.prototype, 'invoke').returns({
promise: () => Promise.resolve({lambda_invoke_result}) //
})
Run Code Online (Sandbox Code Playgroud)
这样,我得到了错误:Cannot stub non-existent property invoke。
示例实现:
const AWS = require("aws-sdk")
AWS.config.update({region: 'us-west-2'})
const dynamodb = new AWS.DynamoDB.DocumentClient()
const lambda = new AWS.Lambda()
// lambda function handler
exports.handler = async (event) => {
let result = await dynamodb.get({/* some get config */}).promise()
// do stuff ...
// kick off next …Run Code Online (Sandbox Code Playgroud) 如何模拟或存根process.argv以便它可以返回特定参数?
我尝试像这样使用Sinon.js :
beforeEach(
() => {
sandbox.stub(
process.argv.slice(2)[0], 'C:\\home\\project\\assignment'
).value('--local');
}
);
afterEach(
() => {
sandbox.restore();
}
);
test(
'it replace value of node argument',
() => {
assert(process.argv.slice(2)[0], '--local');
}
);
Run Code Online (Sandbox Code Playgroud)
但我不断收到一个错误,提示试图'C:\\home\\project\\assignment'存根undefined.
创建一个在重复调用时返回不同值的存根似乎很简单: 是否可以在单个测试中对方法进行两次存根以返回不同的结果?
但是,我如何使用解析多个调用的异步方法来做到这一点?
sinon(module, "myFunction")
.resolves('a')
.resolves('b')
Run Code Online (Sandbox Code Playgroud)
需要明确的是,在上面的片段中,第二个解析会覆盖第一个,因此它总是返回“b”。我想要“myFunction”首先解析“a”然后解析“b”的行为。
说我有这个功能:
function doSomething(n) {
for (var i = 0; i < n; i++) {
doSomethingElse();
}
}
Run Code Online (Sandbox Code Playgroud)
如何测试该doSomethingElse函数是否被调用n次?
我尝试过类似的东西:
test("Testing something", function () {
var spy = sinon.spy(doSomethingElse);
doSomething(12);
equal(spy.callCount, 12, "doSomethingElse is called 12 times");
});
Run Code Online (Sandbox Code Playgroud)
但这似乎不起作用,因为你必须在doSomething()调用原始时调用间谍doSomethingElse().如何使用QUnit/sinon.js完成这项工作?
编辑
也许这甚至不是一个好主意?这是否属于'单元测试',因为调用了另一个函数?
我正在测试骨干视图,它具有以下功能:
attachSelect: function(id, route) {
console.log(id);
console.log(route);
this.$(id).select2({
ajax: {
url: route,
dataType: 'json',
results: function(data) {
var results = _.map(data, function(item) {
return {
id: item.id,
text: item.title
};
});
return {
results: results
};
},
cache: true
}
});
}
Run Code Online (Sandbox Code Playgroud)
我需要重写(模拟)这个功能,看起来像:
attachSelect: function(id, route) {
console.log(id);
console.log(route);
}
Run Code Online (Sandbox Code Playgroud)
怎么做 ?
我已经在React中开发了一段时间用于我的工作,但最近我被要求使用伊斯坦布尔获得一些~100%测试覆盖率的应用程序.在过去的几天里,我已经为这个应用程序编写了160多个测试,但是我无法覆盖代码的某些部分.我在覆盖AJAX调用,setTimeout回调和需要其他组件正常运行的组件方法方面遇到的问题最多.我读过几个SO问题无济于事,我相信这是因为我正在接近这个问题.我正在使用Enzyme,Chai断言,Mocha,伊斯坦布尔报道,sinon为间谍,并且正在考虑nock,因为我不能让sinon fakeServer工作.
这是有问题的组件方法:
_getCategoriesFromServer() {
const _this = this;
sdk.getJSON(_this.props.sdkPath, {
itemsperpage: 10000
}).done(function(data) {
_this.setState({
isLoaded: true,
categories: _this.state.categories.concat(data)
});
});
Run Code Online (Sandbox Code Playgroud)
}
以下是该组件的测试:
it('should call _getCategoriesFromServer', () => {
sinon.spy(CategoryTree.prototype, '_getCategoriesFromServer');
wrapper = mount(<CategoryTree {...props} />);
expect(CategoryTree.prototype._getCategoriesFromServer.calledOnce).to.be.true;
});
Run Code Online (Sandbox Code Playgroud)
sdk只是一个使用getJSON构造jQuery API调用的模块.我的测试覆盖了函数调用,但它没有覆盖这里看到的.done回调:
所以我的问题是,我怎样才能正确测试.done?如果有人有文章,教程,视频,任何解释如何正确测试组件方法的东西,我真的很感激!
第二个问题是,我如何测试一个作为prop组件传递给子组件的方法?根据测试覆盖率要求,我必须测试该方法,但其唯一目的是传递给子组件以用作onClick.这很好,但onClick依赖于另一个AJAX调用返回子组件中的数据.我最初的冲动是只使用酶.find找到onClick并模拟一个click事件,但是那个onClick的元素不存在,因为AJAX调用没有带回测试环境中的数据.如果你读到这里,我向你致敬.如果你能提供帮助,我感谢你!
我更喜欢重构,所以在同一个文件中有样式,因为我的html让我烦恼.
我不想使用<style>填充了css的html标签,这些标签与我的每个组件中的函数和方法交织在一起.
我想继续使用sass或scss文件.(单个文件是我自己的+ bootstrap和我在网上提取的其他通用scss文件).
我该怎么做呢?
我试过了 :
import Vue from 'vue'
import App from './App'
import router from './router'
import './assets/scss/App.scss' <- this is the line I added that broke the app.
Vue.config.productionTip = false
new Vue({
el: '#app',
router,
template: '<App/>',
components: { App }
})
Run Code Online (Sandbox Code Playgroud)
在我的main.js里面
但是我收到了404错误:
更新:
看看这个:在Webpack + VueJs中链接样式表和要求它们有什么区别?好奇我是否应该切换到使用链接?我尝试过,但我不知道应该把它放在哪里.
我应该指明我从这个脚手架开始:https://github.com/vuejs/vue-cli
第二次更新:
我一直在关注指南:https://medium.com/@mahesh.ks/using-sass-scss-in-vue-js-2-d472af0facf9
如果创建具有以下内容的文件
const validateEmail = email => {
sendEmail(email);
};
const sendEmail = email => {
return true;
};
module.exports = {
validateEmail,
sendEmail,
};
Run Code Online (Sandbox Code Playgroud)
并尝试测试第二个功能的测试...
it('Should call sendEmail if a valid email is passed', () => {
let sendEmailSpy = sinon.stub(checkEmail, 'sendEmail');
checkEmail.validateEmail('acorrectemail@therightformat.com');
assert.isTrue(sendEmailSpy.called);
});
Run Code Online (Sandbox Code Playgroud)
它仍然调用sendEmail函数,测试失败
但是,如果我这样写module.exports:
module.exports = {
validateEmail(email) {
this.sendEmail(email);
},
sendEmail(email) {
return true;
},
};
Run Code Online (Sandbox Code Playgroud)
它正确地存根...为什么?
sinon ×10
javascript ×6
node.js ×4
mocha.js ×3
unit-testing ×3
ava ×1
aws-lambda ×1
backbone.js ×1
enzyme ×1
nock ×1
qunit ×1
reactjs ×1
sass ×1
typescript ×1
vue.js ×1
webpack ×1