2016-09-23 55 views
2

我一直寫燼測試是這樣的:嵌套燼驗收測試助手

test('should add new post', function(assert) { 
    visit('/posts/new'); 

    fillIn('input.title', 'My new post'); 
    click('button.submit'); 

    andThen(() => { 
    assert.equal(find('ul.posts li:first').text(), 'My new post') 
    }); 

    click('button.edit'); 
    fillIn('input.title', 'My edited post'); 
    click('button.submit'); 

    andThen(() => { 
    assert.equal(find('ul.posts li:first').text(), 'My edited post') 
    }); 
}); 

,但我也看到測試寫着「嵌套」的風格是這樣的:

test('should add new post', function(assert) { 
    visit('/posts/new'); 

    fillIn('input.title', 'My new post'); 
    click('button.submit'); 

    andThen(() => { 
    assert.equal(find('ul.posts li:first').text(), 'My new post') 

    click('button.edit'); 
    fillIn('input.title', 'My edited post'); 
    click('button.submit'); 

    andThen(() => { 
     assert.equal(find('ul.posts li:first').text(), 'My edited post') 
    }); 
    }); 
}); 

是一種比另一種更好還是更正?第一種風格會成爲競爭條件的來源嗎?

我查閱了一些開源的餘燼應用在GitHub上,看到大部分做我的方式做到這一點:

https://github.com/cowbell/splittypie/blob/master/tests/acceptance/event-test.js

,這裏是嵌套的例子:

https://github.com/HospitalRun/hospitalrun-frontend/blob/master/tests/acceptance/admin-test.js

+0

沒有理由在'andThen'中包裝[異步助手](https://guides.emberjs.com/v2.8.0/testing/acceptance/)。 – steveax

+0

這是我的理解,那麼爲什麼有人會這麼做?我能想象的唯一原因是,如果你包裝它,你可以在異步幫助器上設置一個斷點,否則你不能。 –

回答

0

您要嵌套的一些原因有:

  • 如果如果需要

特定序列按照測試依賴於初始測試

  • 我個人更喜歡用assert.async()

    例如,沿着嵌套:

    test('should add new post', function(assert) { 
        var done = assert.async(); 
        visit('/posts/new'); 
    
        fillIn('input.title', 'My new post'); 
        click('button.submit'); 
    
        andThen(() => { 
        assert.equal(find('ul.posts li:first').text(), 'My new post') 
    
        click('button.edit'); 
        fillIn('input.title', 'My edited post'); 
        click('button.submit'); 
    
        andThen(() => { 
         assert.equal(find('ul.posts li:first').text(), 'My edited post') 
         done(); 
        }); 
        }); 
    }); 
    

    從我個人經驗和然後並不總是如預期般發生,特別是如果你的開發代碼中有一個計時器,所以使用done可以確保我所有的測試都成功完成並且順序完成