简体   繁体   English

Mocha测试异步功能

[英]Mocha tests for asynchronous functions

I'm writing a node wrapper to interact with an external api and am having a difficult time testing the asynchronous createJob method. 我正在编写一个节点包装程序以与外部api交互,并且在测试异步createJob方法时遇到了困难。 Below is the test case code: 下面是测试用例代码:

api_key = "test_0dc8d51e0acffcb1880e0f19c79b2f5b0cc"

lob     = require('../src/lob')(api_key)
should  = require("should")
chai    = require("chai")

data = 
  name: "test name"
  to: "Bob"
  from: "Alice"
  object1: "foo"
  object2: "bar"

describe "Job", ->
  @timeout(50000)
  describe "create", ->
    it "should create a job with address_id", (done) ->
      lob.jobs.createJob data, (new_job) ->
        new_job.should.not.be.empty
        new_job['name'].should.equal(data['name'])
        done()

Edit 编辑

The above code resolves the issue 上面的代码解决了这个问题

(Answer in coffeescript. If you'd like to convert coffee to js use http://coffeescript.org/ , then the Try CoffeeScript tab.) (coffeescript中的答案。如果要将咖啡转换为js,请使用http://coffeescript.org/ ,然后使用Try CoffeeScript选项卡。)

If you're testing asynch code you'll need to use the done pattern: 如果要测试异步代码,则需要使用done模式:

describe "User", ->
  describe "#save()", ->
    it "should save without error", (done) ->
      user = new User("Luna")
      user.save done

http://visionmedia.github.io/mocha/ under "Asynchronous code". http://visionmedia.github.io/mocha/在“异步代码”下。 Looks like createJob is returning true because the test is zipping through the code to send the post etc. and saying "yep, I sent all that stuff like you asked!". 看起来createJob返回的是true,因为测试正在压缩代码以发送帖子等,然后说“是的,我按照您的要求发送了所有内容!”。

I'd recommend Martin Fowler's article on testing asynch js code with mocha: http://martinfowler.com/articles/asyncJS.html . 我建议马丁·福勒(Martin Fowler)的文章关于使用Mocha测试异步js代码: http : //martinfowler.com/articles/asyncJS.html

I've got a chunk of code that tests retrieval of a user from the database (using sinon for stubbing). 我有一段代码测试从数据库中检索用户的代码(使用sinon进行存根)。 The real code connects to the db then calls the onSuccess with the user's configuration: onSuccess(config) 实际代码连接到数据库,然后使用用户的配置调用onSuccess: onSuccess(config)

  describe 'Config', ->
    orgId = 'a'
    errorHandler = ((msg) -> (throw msg))
    beforeEach ->
      readConfig = sinon.stub(sdl , 'getConfig')
      readConfig.callsArgOnWithAsync(2, configSource, JSON.parse(jsonConfig))
    afterEach ->
      configSource.getConfig.restore()

... later ...稍后

  configSource.getConfig('520bc323de4b6f7845543288', errorHandler, (config) ->
      config.should.not.be.null
      config.should.have.property('preferences')
      done()
  )

Don't consider this like an answer to the op but a bonus for the one marked as correct. 不要认为这是对操作的回答,而是对标记为正确操作的人的奖励。

Just to complete @jcollum answer here's the Javascript version of him code: 只是为了完成@jcollum回答,这是他代码的Javascript版本:

describe('User', function(){
    describe('#save()', function(){
        it("should save without error", function(done){
            var _user = new User("Moon");
            _user.save;
            done();
        });
    });
 });

It's pretty evident but maybe some newbies would need this addendum. 这很明显,但也许有些新手需要此附录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM