0

私はJasmine-jQueryの初心者です。フィクスチャ HTML を使用しようとしましたが、テストに合格しません。

フィクスチャ.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"

ジャスミン 結果:

Expected '<p id="0"> </p>' to have text 'text'.

ご親切にありがとうございました。

4

1 に答える 1

1
root = exports ? this
class root.FakeCodeForQuestion
  addTextToParagraph0: ->
    $('p0').text "text"

こんにちは、ここでの問題は、セレクターが p0 の ID を持つ要素を参照していないことです。つまり、$('body') が body 要素を選択する方法と同様に、存在しない要素 p0 を参照しています。

代わりに $('#p0') にしたい

于 2013-03-22T01:03:03.730 に答える