2014-06-16 126 views
2

第二個測試,說h3元素存在,應該明顯失敗,但不會。這是怎麼回事?摩卡通過測試,應該失敗(ember-mocha-adapter)

使用Mocha,Chai,Ember和ember-mocha-adapter,我創建了這個簡單的示例:http://jsfiddle.net/signer247/UD2D3/4/


HTML

<div id="mocha"></div> 
<hr/> 
<div id="ember-testing"></div> 

<script type="text/x-handlebars" data-template-name="application"> 

    <h1>Ember.js Testing with Ember Mocha Adapter</h1> 

</script> 


的CoffeeScript

App = Em.Application.create() 

App.Router.map -> 
    @route 'index', path: '/' 

App.rootElement = '#ember-testing'; 
App.setupForTesting() 
App.injectTestHelpers() 

Ember.Test.adapter = Ember.Test.MochaAdapter.create() 

chai.should() 

describe 'Changing a site via visit in the test with andThen helper', -> 

    beforeEach -> 
     App.reset() 
     visit('/') 

    it 'should work', -> 
     andThen -> 
      $c = $(App.rootElement) 
      $c.find('h1').should.exist 

    it 'should fail', -> 
     andThen -> 
      $c = $(App.rootElement) 
      $c.find('h3').should.exist 

$(document).ready -> 
    mocha.run(); 


我的jsfiddle:http://jsfiddle.net/signer247/UD2D3/4/

我建我的jsfiddle關閉這個例子:http://jsfiddle.net/UD2D3/1/

這是餘燼,摩卡適配器:https://github.com/teddyzeenny/ember-mocha-adapter

回答

2

沒有摩卡這裏的專家,但應存在看起來像它只是證明從jquery選擇器返回的結果存在,並且它們是,它們只是空的。即使這個例子不能正常工作,你可以將任何東西放入應該存在的基於jquery選擇器並返回通過。

it 'should work', -> 
    andThen -> 
     $c = $(App.rootElement) 
     $c.find('h1').length.should.equal(1) 

it 'should fail', -> 
    andThen -> 
     $c = $(App.rootElement) 
     console.log($c.find('h3')); 
     $c.find('h3').length.should.equal(1) 

http://jsfiddle.net/3AQUN/

+1

太感謝你了!我已將我的測試從「should.exist」更改爲「length.should.equal(1)」,並且所有內容都按預期工作。 – Jacquerie

+0

真棒,很高興聽到 – Kingpin2k