Question

I am newbie of Jasmine-jQuery. I tried to use fixture HTML but test doesn't pass.

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"

Jasmine Result:

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

Thank you for your kindness.

Was it helpful?

Solution

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 i.e. similar to how $('body') selects the body element.

You want it to be $('#p0') instead

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top