【问题标题】:Asynchronous Mocha Testing with Node and Backbone使用 Node 和 Backbone 进行异步 Mocha 测试
【发布时间】:2014-08-14 22:32:39
【问题描述】:

我正在使用beforeEach 和afterEach 钩子为测试中的每个it 块实例化一个带有模型和用户的视图。但是在第一个it 块user.attributes 的beforeEach 钩子中是空的。在第二个中,user.attributes 有一个属性,即使它是用一个空用户实例化的。为什么用属性实例化第二个用户?

describe("TestView", ->
    beforeEach((done)->
        @view = new TestView({model: new SomeModel(), user: new User()})
        console.log(@view.user)
        @view.render(done)
    )

    afterEach(->
        @view?.remove()
    )

    it("should initialize and render without errors", ->
        expect(@view).to.be.ok
    )

    it("should get the test class after being clicked", (done)->
        expect($('.some-class').hasClass('test')).to.be.equal(false)
        @view.$el.click()
        setTimeout(->
            expect($('.some-class').hasClass('test')).to.be.equal(true)
            done()
        , 100) # give it 100ms to let the event handlers do their things
    )
)

【问题讨论】:

    标签: backbone.js asynchronous mocha.js


    【解决方案1】:

    console.log() 在浏览器的 Javascript 控制台中为您提供一个对象检查器,但检查器不会立即填充所有当前数据。当您可以在控制台中展开用户对象时,点击已经发生。

    你可以通过说得到一个更静态的快照来检查

    console.log(user.clone())
    

    甚至

    console.log(JSON.parse(JSON.stringify(user))
    

    或者您可以只记录您感兴趣的特定属性:

    console.log(user.attribute)
    

    【讨论】:

      猜你喜欢
      • 2014-03-19
      • 1970-01-01
      • 2016-04-05
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 2014-02-06
      相关资源
      最近更新 更多