简体   繁体   English

使用Jasmine-jQuery,我尝试更改设备HTML,但是它不起作用

[英]With Jasmine-jQuery I tried to change fixture HTML but it doesn't work

I am newbie of Jasmine-jQuery . 我是Jasmine-jQuery的新手。 I tried to use fixture HTML but test doesn't pass. 我尝试使用夹具HTML,但测试未通过。

fixture.html: Fixture.html:

<html>
<body>
  <p id="0">
  </p>
</body>
</html>

fake_code_for_question_spec.coffee: 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: fake_code_for_question.coffee:

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

Jasmine Result: 茉莉花结果:

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

Thank you for your kindness. 谢谢你的好意。

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

Hi again, the issue here is your selector doesnt refer to an element with the id of p0 rather it refers to a non-existant element p0 ie similar to how $('body') selects the body element. 再次嗨,这里的问题是您的选择器未引用ID为p0的元素,而是引用了不存在的元素p0,即类似于$('body')选择body元素的方式。

You want it to be $('#p0') instead 您希望它是$('#p0')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM