【问题标题】:Mocha passes test that should fail (ember-mocha-adapter)Mocha 通过了应该失败的测试(ember-mocha-adapter)
【发布时间】:2014-08-06 19:46:21
【问题描述】:

第二个测试,说存在一个 h3 元素,显然应该失败,但没有。怎么回事?

使用 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>


咖啡脚本

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/

这是 ember-mocha 适配器:https://github.com/teddyzeenny/ember-mocha-adapter

【问题讨论】:

    标签: javascript ember.js coffeescript mocha.js chai


    【解决方案1】:

    这里没有 mocha 专家,但应该存在 看起来它只是证明从 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/

    【讨论】:

    • 非常感谢!我已将测试从“should.exist”更改为“length.should.equal(1)”,一切都按预期工作。
    • 太棒了,很高兴听到
    猜你喜欢
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-04-17
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 2013-01-30
    相关资源
    最近更新 更多