我正在尝试使用Qunit来测试一些代码,但我在Ajax调用方面遇到了一些问题。 我甚至无法使用jQuery方法使用最简单的Ajax调用来正确测试它们。 问题似乎是,无论我做什么,都会在textResponse上附加一个尾随空格。
我的初始代码是这样的
asyncTest('Ajax calls', function() { expect(1); $.get('ajax.txt', {}, function(response) { equal(response, 'foo', 'Ajax calls work correctly'); }); setTimeout(function() { start(); }, 600); });其中ajax.txt是一个包含ajax.txt字符foo的文本文件。 报告失败了
Ajax调用正常工作,预期:“foo”结果:“foo”,diff:
“foo”“foo”
然后我尝试了以下内容:
我测试了“foo”(包括尾随空格) 我在测试之前完成了response.replace(' ', '') 我改变了ajax.txt文件的字体编码 我每次都在清理缓存时在Firefox和Chrome中测试过它 我已经手动测试了警报内的相等性,即使是使用==比较但在任何情况下我都无法得到一场比赛。 例如,在第一个版本中,我得到了令人费解的答案
Ajax调用正常工作,预期:“foo”结果:“foo”,diff:“foo”
我现在有点生气了。 我可能做错了什么?
I am trying to use Qunit to test some code, but I have some problems with Ajax calls. I cannot even get them to test correctly with the simplest Ajax call using jQuery methods. The problems seems to be that a trailing space is appended to the textResponse, no matter what I do.
My initial code was something like
asyncTest('Ajax calls', function() { expect(1); $.get('ajax.txt', {}, function(response) { equal(response, 'foo', 'Ajax calls work correctly'); }); setTimeout(function() { start(); }, 600); });where ajax.txt is a text file containing olny the characters foo. This test fails, reporting
Ajax calls work correctly, expected: "foo" result: "foo ", diff:
"foo""foo "
I have then tried the following:
I have tested against "foo " (including a trailing space) I have done response.replace(' ', '') before testing I have varied the font encoding of the ajax.txt file I have tested it both in Firefox and Chrome, each time cleaning the cache I have manually tested for equality inside an alert, even with == comparisonbut in no case I was able to get a match. For instance in the first variant I got the puzzling answer
Ajax calls work correctly, expected: "foo " result: "foo ", diff: "foo "
I am now going slightly mad. What could I have been possibly doing wrong?
最满意答案
您可以$.trim() (jQuery修剪,因为IE <9本身没有它)结果,如下所示:
equal($.trim(response), 'foo', 'Ajax calls work correctly');为什么会这样? 这可能是一个格式化错误,例如Unix与Windows行结尾在你身上蔓延。
You can $.trim() (jQuery trim, since IE<9 doesn't have it natively) the result, like this:
equal($.trim(response), 'foo', 'Ajax calls work correctly');Why is this happening? It's likely a formatting error, e.g. Unix vs Windows line endings that are creeping in there on you.
更多推荐
发布评论