2014-06-16 89 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