我有 3 个测试,每个测试都测试不同的方法。
it('test function1', function() {
spyOn(document, 'getElementById');
// ... some code to test function1
expect(document.getElementById).toHaveBeenCalled();
});
it('test function2', function() {
spyOn(document, 'getElementById');
// ... some code to test function2
expect(document.getElementById).toHaveBeenCalled();
});
it('test function3', function() {
spyOn(document, 'getElementById');
// ... some code to test function3
expect(document.getElementById).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)
但是当我运行这些测试时,我收到以下错误:getElementById has already been spied upon。有人可以解释为什么即使间谍位于不同的测试套件中我也会收到此错误以及如何修复它。
我对这个flot API很新.我希望有虚线网格线/刻度线而不是实线X轴和Y轴.谁能帮我这个??
提前致谢!
通过单击另一个外部按钮来调用remove()on id main.问题是如果用户单击btn1并快速按下该外部按钮,则会在事件处理程序之前调用remove btn1.因此,在删除div之后加载弹出窗口.有没有一种方法可以在单击删除事件处理程序时停止加载请求?我试着jqXHR.abort()调用remove方法,但是这不起作用,因为在发送ajax之前调用了remove.
有许多按钮,如btn1,它将发送ajax请求加载HTML和少数HTMlL文件,例如a.html将加载一些脚本文件a.js,如将执行.如果脚本引用了一些已删除的变量remove(),则会出现TypeError.
<div id="base">
<div id="main">
<!-- some more HTML elements -->
<button id="btn1"></button>
</div>
<div id ="popup">
</div>
</div>
<script>
var xhr;
$("#btn1").on("click", function(){
xhr = $.ajax(
url: "a.html",
success: function(){
//do something
}),
type: "GET"
});
$("#main").on("remove", function(){
// delete all resources,etc.
xhr.abort();
});
</script>
Run Code Online (Sandbox Code Playgroud) 我有一个脚本 abc.js,它有一个按钮的事件处理程序,如下所示:
$("button").on("click", function(e){
$("script[src='abc.js']").remove();
});
Run Code Online (Sandbox Code Playgroud)
调用执行脚本上的 remove 时会发生什么?脚本会被卸载,还是会停止执行?当脚本从其他脚本文件中删除时会发生什么?
假设我有一段 JavaScript 代码:
function modifiesLocalStorage() {
var someBoolean = false;
if(localStorage.getItem('someKey') === 'true'){
localStorage.removeItem('someKey');
someBoolean = true;
}
return someBoolean;
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个茉莉花测试来测试这个方法:
it('should return true', function(){
spyOn(localStorage, 'removeItem');
spyOn(localStorage, 'getItem').and.returnValue('true');
var returnValue = modifiesLocalStorage();
expect(localStorage.getItem).toHaveBeenCalled(); //Error in this line
expect(returnValue).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)
执行此测试时,我收到以下错误:
Error: <toHaveBeenCalled> : Expected a spy, but got Function.
此错误是什么以及如何修复它?
我正在无头模式下使用 Firefox 45.9.0 浏览器来运行测试。
如何测试这个功能:
var self = this;
self.someVariable = "initial value";
self.test = function() {
self.functionWhichReturnsPromise().then(function() {
self.someVariable = "new value";
});
}
Run Code Online (Sandbox Code Playgroud)
我有测试用例像下面,我知道这是错误的,因为之前承诺解决,assert语句会被执行茉莉:
it("should test the function test", function (done) {
var abc = new ABC();
abc.test();
expect(abc.someVariable).toBe('new value');
});
Run Code Online (Sandbox Code Playgroud)
请注意,我不想使用setTimeout()或任何睡眠方法.
javascript ×5
jquery ×4
jasmine ×3
html ×2
ajax ×1
firefox ×1
flot ×1
grid ×1
jasmine2.0 ×1
promise ×1