admin管理员组文章数量:1310221
This is a Rails app. I'm using the Jasmine gem with Jasmine-Jquery.
Here's my JS in asset pipeline:
$selector = function() {
$('#filters li').click(function() {
$('#filters li').siblings().removeClass('selected');
$(this).addClass('selected');
});
}
$selector();
Here's my fixture called workgrid_spec.html:
<div class="row">
<div class="large-12 columns">
<ul id="filters">
<li class="identity" data-filter=".identity">Identity</li>
<li>//</li>
<li data-filter=".print">Print</li>
<li>//</li>
<li data-filter=".web">Web</li>
<li>//</li>
<li data-filter=".video">Video</li>
<li>//</li>
<li class="selected" data-filter="*">See All</li>
</ul>
</div>
</div>
And here's my spec file:
describe("Make sure the workgrid works right", function() {
beforeEach(function() {
loadFixtures('workgrid_spec.html');
});
describe("when a filter is clicked", function() {
it("is highlighted in the list with the 'selected' class", function() {
$('.identity').click();
expect('.identity').toHaveClass('selected');
});
});
});
And here's the error I'm getting:
Error: Expected '.identity' to have class 'selected'.
at new jasmine.ExpectationResult (http://localhost:8888/__jasmine__/jasmine.js:114:32)
at null.toHaveClass (http://localhost:8888/__jasmine__/jasmine.js:1235:29)
at null.<anonymous> (http://localhost:8888/__spec__/workgrid_spec.js:12:24)
This is a Rails app. I'm using the Jasmine gem with Jasmine-Jquery.
Here's my JS in asset pipeline:
$selector = function() {
$('#filters li').click(function() {
$('#filters li').siblings().removeClass('selected');
$(this).addClass('selected');
});
}
$selector();
Here's my fixture called workgrid_spec.html:
<div class="row">
<div class="large-12 columns">
<ul id="filters">
<li class="identity" data-filter=".identity">Identity</li>
<li>//</li>
<li data-filter=".print">Print</li>
<li>//</li>
<li data-filter=".web">Web</li>
<li>//</li>
<li data-filter=".video">Video</li>
<li>//</li>
<li class="selected" data-filter="*">See All</li>
</ul>
</div>
</div>
And here's my spec file:
describe("Make sure the workgrid works right", function() {
beforeEach(function() {
loadFixtures('workgrid_spec.html');
});
describe("when a filter is clicked", function() {
it("is highlighted in the list with the 'selected' class", function() {
$('.identity').click();
expect('.identity').toHaveClass('selected');
});
});
});
And here's the error I'm getting:
Error: Expected '.identity' to have class 'selected'.
at new jasmine.ExpectationResult (http://localhost:8888/__jasmine__/jasmine.js:114:32)
at null.toHaveClass (http://localhost:8888/__jasmine__/jasmine.js:1235:29)
at null.<anonymous> (http://localhost:8888/__spec__/workgrid_spec.js:12:24)
Share
Improve this question
asked May 30, 2013 at 15:50
Scott MagdaleinScott Magdalein
3911 gold badge3 silver badges13 bronze badges
2 Answers
Reset to default 3The problem is that you $selector
function is called before the fixture is loaded, so the event can't added to the element. You have to call the function in your test again or use jquerys delegate
function to bind the event.
As described by Andreas, you need to use your selector like so:
it("is highlighted in the list with the 'selected' class", function() {
$('.identity').click();
expect($('.identity')).toHaveClass('selected');
});
remember to include JQuery in your specs as well, ie.
var $ = require('jquery');
本文标签: javascriptJasmineChecking addClass on clickStack Overflow
版权声明:本文标题:javascript - Jasmine - Checking addClass on click - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741841353a2400518.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论