我正在努力改进单元测试我的JavaScript.我有以下代码:
var categoryVal = $('#category').val();
if (categoryVal === '') {
doSomething();
}
Run Code Online (Sandbox Code Playgroud)
我的测试运行器没有#category页面上的输入,那么我将如何在这里存根/模拟jQuery选择器?我看了两个茉莉和兴农文档,但无法弄清楚如何让他们在这里工作,因为他们的存根对象进行操作,这$是没有的.
我正在mocha测试一些类,我需要创建一个request库的存根.
我正在使用sinon,我能够创建该request.get方法的存根,但我无法创建该request方法的存根(http调用尝试连接到服务器).正如我所读到的,request.get是一个别名,request但是当我存根时request.get它对request调用没有影响.
此代码有效(使用request.get):
在测试中:
request = require 'request'
describe "User test", ->
user = {}
before (done) ->
user = new test.user('Ander', 18)
sinon.stub(request, 'get').yields(null, {statusCode: 200}, 'foo')
done()
after (done) ->
request.get.restore()
done()
it "testing server response", ->
user.getData().should.equal 'ander'
Run Code Online (Sandbox Code Playgroud)
来源:
request = require 'request'
class User
contructor(@name, @age): ->
getData: ->
mydata = ''
request.get 'http://127.0.0.1:8080/', (err, response, …Run Code Online (Sandbox Code Playgroud) 我正在尝试拦截与Sinon.js的调用,这样我就可以进行一些日志记录然后执行原始调用.我没有看到用sinon.spy()做到这一点的方法,但我想我可以用sinon.stub()做到这一点.
我提供了一个自定义功能:
sinon.stub(servicecore.ServiceWrapper.prototype, '_invoke', function(method, name, body, headers, callback) {
console.log('---- ServiceWrapper._invoke called! ----');
// How do I call the original function?
});
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是执行原始函数,所以我的应用程序行为相同.任何的想法?
我正在尝试使用backbone.js,jasmine.js和sinon.js测试按钮单击.但是以下测试用例失败了.我正在使用间谍来追踪它是否被召唤.你能帮我解决这个问题吗?
谢谢.
新任务模板
<script id='new_task_template' type='text/template'>
<input type='text' id='new_task_name' name='new_task_name'></input>
<button type='button' id='add_new_task' name='add_new_task'>Add Task</button>
</script>
Run Code Online (Sandbox Code Playgroud)
NewTaskView
T.views.NewTaskView = Backbone.View.extend({
tagName: 'section',
id: 'new_task_section',
template : _.template ( $("#new_task_template").html() ),
initialize: function(){
_.bindAll( this, 'render', 'addTask');
},
events:{
"click #add_new_task" : "addTask"
},
render: function(){
$(this.el).html( this.template() );
return this;
},
addTask: function(event){
console.log("addTask");
}
});
Run Code Online (Sandbox Code Playgroud)
茉莉花测试案例
describe("NewTaskView", function(){
beforeEach( function(){
this.view = new T.views.NewTaskView();
this.view.render();
});
it("should #add_new_task is clicked, it should trigger the addTask method", function(){
var clickSpy …Run Code Online (Sandbox Code Playgroud) 我想创建一个猫鼬存根save在特定模型的方法,让我创造我的模型的任何实例将调用存根而不是正常的猫鼬的save方法.我的理解是,这样做的唯一方法是将整个模型存根如下:
var stub = sinon.stub(myModel.prototype);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这行代码导致我的测试抛出以下错误:
TypeError: Cannot read property 'states' of undefined
Run Code Online (Sandbox Code Playgroud)
有谁知道这里出了什么问题?
我正在拼凑一个jsTestDriver/Jasmine环境来测试我们的前端代码,我看到很多对Sinon.js的引用,用于独立(或插入)间谍.有人能描述一下Sinon.js给Jasmine不用于测试用户界面的表吗?
我试图在构造函数上创建一个间谍,看看它是否被调用 - 下面是我的测试.我正在使用sinon-chai,因此语法有效,但两个测试都失败了.
var foo = function(arg) {
};
var bar = function(arg) {
var baz = new foo(arg);
};
it('foo initialized inside of this test', function() {
var spy = sinon.spy(foo);
new foo('test');
expect(spy).to.be.called;
expect(spy).to.be.calledWith('test');
});
it('foo initialized by bar()', function() {
var spy = sinon.spy(foo);
bar('test');
expect(spy).to.be.called;
expect(spy).to.be.calledWith('test');
});
Run Code Online (Sandbox Code Playgroud) 目前伊斯坦布尔只为我的测试中使用的文件生成了覆盖范围,这是可以的,但似乎无法实现覆盖的目的.
我没有Istanbul配置,并通过npm test以下脚本字符串调用它:
$ istanbul cover _mocha -- -R dot --check-leaks --recursive test/
有没有办法为我的所有源代码生成报道?
我正在尝试使用sinon.js测试骨干应用程序.但不幸的是我因为错误而无法使用间谍方法:
TypeError: 'undefined' is not a function (evaluating 'sinon.spy()')
Run Code Online (Sandbox Code Playgroud)
以下是重现错误的步骤:
cd test && bower install sinon<script src="bower_components/sinon/lib/sinon.js"></script>在test/spec/test.js中创建间谍:
describe('Give it some context', function () {
describe('maybe a bit more context here', function () {
it('should run here few assertions', function () {
var spy = sinon.spy();
spy.should.be.ok;
});
});
});
Run Code Online (Sandbox Code Playgroud)用grunt运行测试: grunt test
测试将失败并显示错误.
谁能帮忙找出问题所在?
在Sinon,我可以做以下事情:
var myObj = {
prop: 'foo'
};
sinon.stub(myObj, 'prop').get(function getterFn() {
return 'bar';
});
myObj.prop; // 'bar'
Run Code Online (Sandbox Code Playgroud)
但是我怎么能和Jest一样呢?我不能用类似的东西覆盖函数jest.fn(),因为它不会取代getter
"无法设置get的值"
sinon ×10
javascript ×6
jasmine ×3
mocha.js ×3
node.js ×3
backbone.js ×2
jquery ×2
bdd ×1
chai ×1
istanbul ×1
jestjs ×1
mocking ×1
mongoose ×1
reactjs ×1
unit-testing ×1