我是 Jasmine-jQuery 的新手。我试图使用fixture HTML但测试没有通过。
fixture.html:
<html>
<body>
<p id="0">
</p>
</body>
</html>
fake_code_for_question_spec.coffee:
describe "FakeCodeForQuestion", ->
describe "with HTML fixture", ->
beforeEach ->
loadFixtures "fixture.html" ### Load Fixture
@obj = new FakeCodeForQuestion
describe "#addText", ->
beforeEach ->
@obj.addTextToParagraph0() ### Change DOM
it "should add text", ->
expect($('p#0')).toHaveText "text" ### Get Changed DOM
fake_code_for_question.coffee:
root = exports ? this
class root.FakeCodeForQuestion
addTextToParagraph0: ->
$('p0').text "text"
Jasmine结果:
Expected '<p id="0"> </p>' to have text 'text'.
谢谢你的善意。
答案 0 :(得分:1)
root = exports ? this
class root.FakeCodeForQuestion
addTextToParagraph0: ->
$('p0').text "text"
嗨再次,这里的问题是你的选择器不引用id为p0的元素,而是引用一个不存在的元素p0,即类似于$('body')如何选择body元素。
你希望它是$('#p0')而不是