【问题标题】:With Jasmine-jQuery I tried to change fixture HTML but it doesn't work使用 Jasmine-jQuery 我尝试更改夹具 HTML,但它不起作用
【发布时间】:2023-04-07 21:05:01
【问题描述】:

我是 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"

茉莉花结果:

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

谢谢你的好意。

【问题讨论】:

    标签: javascript coffeescript tdd jasmine jasmine-jquery


    【解决方案1】:
    root = exports ? this
    class root.FakeCodeForQuestion
      addTextToParagraph0: ->
        $('p0').text "text"
    

    您好,这里的问题是您的选择器没有引用 id 为 p0 的元素,而是引用了不存在的元素 p0,即类似于 $('body') 选择 body 元素的方式。

    你希望它改为 $('#p0')

    【讨论】:

      猜你喜欢
      • 2013-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-26
      • 2020-06-28
      • 1970-01-01
      相关资源
      最近更新 更多