2017-04-09 39 views
0

此代码让我摩卡测试通过而不会出现错误:摩卡功能之前 - 没有花括号错误的lambda;拉姆达用花括号工作

before(done => { 
    mockgoose 
    .prepareStorage() 
    .then(() => mongoose.connect('mongodb://example.com/TestingDB')) 
    .then(done) 
}) 

it('passes', done => done()) 

但在before块移除花括号导致错误:

before(done => 
    mockgoose 
    .prepareStorage() 
    .then(() => mongoose.connect('mongodb://example.com/TestingDB')) 
    .then(done) 
) 

it('passes', done => done()) 

1) "before all" hook 

0 passing (2s) 
1 failing 

1) "before all" hook: 
    Error: Resolution method is overspecified. Specify a callback *or* return a Promise; not both. 
    at process._tickCallback (internal/process/next_tick.js:109:7) 

有谁知道为什么吗?如果需要更多的上下文,我可以承担责任。

回答

3

它是这么说的就在那里,你以前不返回任何东西,你只是使用done指定当任务完成。现在你回来了一个Promise(我假设的模仿电话的结果),它是混淆摩卡。

+0

Ahhhh,是的。通过使用没有花括号的lambda,我有一个隐含的回报!所以,没有大括号,我回来了一个Promsie。通过提供'done',我还指定了一个回调。我只是摆脱“完成”,并在我的快乐方式。谢谢!我是这个Javascript场景的新手:) – jrahhali