【问题标题】:With Jasmine-jQuery I tried to use fixture HTML but test doesn't pass使用 Jasmine-jQuery,我尝试使用夹具 HTML,但测试未通过
【发布时间】:2013-03-06 12:04:05
【问题描述】:

我是 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')).toHavaText "text"   ### Get Changed DOM

fake_code_for_question.coffee:

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

茉莉花结果:

FakeCodeForQuestion with HTML fixture #addText should add text.
TypeError: Object [object Object] has no method 'toHavaText'
TypeError: Object [object Object] has no method 'toHavaText'
    at null.<anonymous> (http://localhost:8888/__spec__/p130318_fake_code_for_question_spec.js:14:35)
    at jasmine.Block.execute (http://localhost:8888/__jasmine__/jasmine.js:1064:17)
    at jasmine.Queue.next_ (http://localhost:8888/__jasmine__/jasmine.js:2096:31)
    at goAgain (http://localhost:8888/__jasmine__/jasmine.js:2086:18)

谢谢你的好意。

【问题讨论】:

    标签: javascript jquery coffeescript jasmine jasmine-jquery


    【解决方案1】:
    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')).toHavaText "text"   ### Get Changed DOM
    

    你得到的结果,没有方法 'toHavaText' 给你指示,你想要 toHaveText 代替

    【讨论】:

      【解决方案2】:

      我认为你的文件夹结构不符合 jasmine-jquery。 loadFixtures() 使用此目录路径 spec/javascripts/fixtures。如果你进入 jasmin-jquery 代码库,查找fixturesPath,你会发现它们都使用spec/javascripts/fixtures。但是您可以使用jasmine.getFixtures().fixturesPath = "[your directory path here]"; 覆盖它。

      如果我理解您的问题,希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2023-04-07
        • 2017-07-23
        • 2013-05-15
        • 1970-01-01
        • 2014-11-15
        • 1970-01-01
        • 2013-02-06
        • 2011-01-17
        • 1970-01-01
        相关资源
        最近更新 更多