使用Jasmine-jQuery我尝试使用fixture HTML但测试没有通过

时间:2013-03-18 07:51:28

标签: javascript jquery coffeescript jasmine jasmine-jquery

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

Jasmine结果:

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)

谢谢你的善意。

2 个答案:

答案 0 :(得分:2)

我认为您的文件夹结构与jasmine-jquery不一致。 loadFixtures()使用此目录路径spec/javascripts/fixtures。如果您进入jasmin-jquery代码库,请查看fixturesPath,您会看到所有这些代码都使用spec/javascripts/fixtures。但您可以使用jasmine.getFixtures().fixturesPath = "[your directory path here]";覆盖此内容。

如果我理解你的问题,我希望这有帮助。

答案 1 :(得分:2)

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'给你指示,你想要改为HaveText