- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我相当确定问题是设置为在 $(document).ready 上运行的 jquery 绑定(bind)没有可用的固定 html。因此,当我发生旨在通过 jquery 函数更改 DOM 的事件时,什么也没有发生,我的测试失败了。我看到了这个问题的“解决方案”here ,但是对我有用的解决方案需要更改我的工作 jquery 函数以绑定(bind) .live 方法而不是 .click 方法。我有两个问题。首先,我不想更改我的工作代码以使测试正确通过。测试框架应该测试代码是否会在应用程序中工作,DOM 加载和 javascript 绑定(bind)以正确的顺序发生。我对解决方案的第二个问题是 .on 和 .delegate 由于某种原因都不起作用,唯一最终起作用的是使用 .live 方法,该方法目前正在被弃用。总之,我想弄清楚如何更改我的测试或测试框架本身,以便在 $(document).ready 中运行的函数之前加载固定装置。
我正在使用 Rails 3.2.8,我设置了两个不同的分支来试验 jasmine 测试。其中一个分支使用 jasminerice gem,另一个使用 jasmine-rails gem。 Javascript 和 jQuery(使用 .live 方法时)测试在这两种设置中都正确通过。
这里是使用 jasminerice 的分支的描述:
以下是我的 Gemfile.lock 文件中描述 jasmine 和 jQuery 设置的行:
jasminerice (0.0.9)
coffee-rails
haml
jquery-rails (2.1.3)
railties (>= 3.1.0, < 5.0)
thor (~> 0.14)
Jasminerice 包括 jasmine-jquery默认情况下扩展。 jasmine-jQuery 扩展提供了我在测试中使用的 toHaveText 方法。
直接位于 spec/javascripts 目录中的测试文件 jasmine_jquery_test.js 包含以下内容:
#= require application
describe ("my basic jasmine jquery test", function(){
beforeEach(function(){
$('<a id="test_link" href="somewhere.html">My test link</a>').appendTo('body');
});
afterEach(function(){
$('a#test_link').remove();
});
it ("does some basic jQuery thing", function () {
$('a#test_link').click();
expect($("a#test_link")).toHaveText('My test link is now longer');
});
it ("does some the same basic jQuery thing with a different trigger type", function () {
$('a#test_link').trigger('click');
expect($("a#test_link")).toHaveText('My test link is now longer');
});
});
describe ('subtraction', function(){
var a = 1;
var b = 2;
it("returns the correct answer", function(){
expect(subtraction(a,b)).toBe(-1);
});
});
位于 app/assets/javascripts 目录中的我的 javascript 文件 tests.js 具有以下内容:
function subtraction(a,b){
return a - b;
}
jQuery (function($) {
/*
The function I would rather use -
$("a#test_link").click(changeTheTextOfTheLink)
function changeTheTextOfTheLink(e) {
e.preventDefault()
$("a#test_link").append(' is now longer');
}
*/
$("a#test_link").live('click', function(e) {
e.preventDefault();
$("a#test_link").append(' is now longer');
});
});
位于同一 app/assets/javascripts 目录中的我的 application.js 文件具有以下内容:
//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require vendor
//= require_tree .
而jasmine-rails分支的描述可以在我的first jasmine test related stackoverflow question的内容中找到.
因此,如果有人知道如何操纵测试,以便 $(document).ready 函数在 fixture 加载后运行,我非常想听听您的想法?
最佳答案
将 jQuery 直接添加到 html fixture 对我来说获得了所需的行为。这不完全是这个问题的答案,但我开始相信这是目前可用的最佳解决方案。我的更改是在 jasmine-rails gem 设置中进行的,我还没有在 jasminerice 分支中尝试过。
这是我的测试文件:
describe ("my basic jasmine jquery test", function(){
beforeEach(function(){
loadFixtures('myfixture.html');
});
it ("does some basic jQuery thing", function () {
$('a#test_link').click();
expect($("a#test_link")).toHaveText('My test link is now longer');
});
it ("does the same basic jQuery thing with a different event initiation method", function () {
$('a#test_link').trigger('click');
expect($("a#test_link")).toHaveText('My test link is now longer');
});
});
describe ('substraction', function(){
var a = 1;
var b = 2;
it("returns the correct answer", function(){
expect(substraction(a,b)).toBe(-1);
});
});
这是我的夹具文件:
<a id="test_link" href="somewhere.html">My test link</a>
<script>
$("a#test_link").click(changeTheTextOfTheLink)
function changeTheTextOfTheLink(e) {
e.preventDefault()
$("a#test_link").append(' is now longer');
}
</script>
关于javascript - 在 javascript 认为文档为 "ready"之前,如何让我的 jasmine 测试装置加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12715207/
根据 jQueryDoc在术语中,.ready() 被称为位于 jQuery.prototype 中的查询选择方法。对于前 $(document).ready() 作为jQueryDoc说:$ 命名空
我见过一些代码,他们只是这样做: $().ready(function() { ... }); 这比执行文档选择器要短,但它是同一件事吗? 最佳答案 略有变化: $(document).rea
各个处理程序的回调函数何时执行? 最佳答案 在主要过程中 当您的应用完成初始化并准备打开浏览器窗口时,将触发app上的“就绪”事件。由于在此之前您无法打开窗口,因此可以使用回调函数来创建Browser
最近发现了head.js图书馆和男孩,我对此感到满意,尽管我仍然对一件事感到有点困惑。 来自 headjs.com: The “DOM ready” event such as $(document)
我在我的代码库中发现代码在另一个 $(document).ready(function() { ... 例如 $(document).ready(function() { // 20
我已经养成了从ready函数开始jquery编码的习惯 $(function(){...}); 并将所有从ready调用的函数放入ready中。 然后我意识到,放入就绪函数中的一些函数可能不需要在那里
在浏览旧代码库时,我发现了以前的软件开发人员正在使用的地方 $(function(a) {}(A || (A = {}))); 即使页面尚未准备好,它仍在执行。在我删除传递的全局变量后它开始工作。代码
我已经看到了两种方式,但哪种更好或者并不重要。 我觉得包装每个语句可能会更干净,但只是想知道如果您有 50 个语句,每个语句都有自己的 document.ready 事件处理程序,是否会有更多回调?
这个问题已经有答案了: Four variations of jQuery ready() -- what's the difference? (4 个回答) 已关闭 4 年前。 我正在编写一些我没有
我们最近遇到了一个错误,jquery document.ready 似乎在 DOM 加载之前触发。事实证明,在调用的第一个项目之后有一些错误的代码 $.ready(function(){}); 这条语
什么应该在 jQuery.ready() 中,什么应该在 jQuery.ready() 之外? 从性能角度来看,我在某处读到,将所有代码包装在 jQuery.ready() 中并不是一种有效的方法。
我实现了一个带有选项列表(工作类别)的页面,单击该页面时应显示数据(工作描述)。我正在使用 BBQ 来处理后台堆栈。 一切正常,除了在用户首次导航到页面时设置初始选择。我的代码被调用到 addClas
解决方案 我有两个 users.js和 users.coffee在我的 Assets 管道中。显然 users.coffee正在阻止 users.js从被加载。确保删除它! 我正在尝试实现 this
我正在研究一个指令,但我不想玩 $document或 $window , 只有 element本身。 之前我有: angular.element($document).ready(function()
我想在移动设备方向改变时使用 $(window).resize 来调用一些函数,我在 $(document).ready 中编写了所有代码,这在我使用 Android 设备时有效,但在使用 iPhon
我有一个使用数据库的 Ionic 应用程序。我想用一个文件的内容填充这个数据库。 这部分我开始工作了。我想创建一个 DB.ready() 事件,很像 $ionicPlatform.ready() 或
我有一个名为“loadTimeTrackersGrid()”的函数,它加载一个弹性网格。设置如下所示: $(document).ready(function () { var edi
我有一个定义数量为 replicas 的部署.如果我的 Pod 准备好/未准备好处理新连接,我使用就绪探针进行通信 - 我的 Pod 在 ready 之间切换/not ready他们一生中的状态。 我
有什么区别: $(document).ready(initialize); 和 $(document).on('ready', initialize); 对我来说,它们似乎以相同的方式工作。 最佳答案
我看到很多项目都在使用 $(document).on('ready', function(e){ //jquery stuff }) 而不是: $( document ).ready(functio
我是一名优秀的程序员,十分优秀!