2015-12-17 2 views
5

Я пытаюсь написать единичный тест, чтобы гарантировать закрытие свойств при закрытии модального. Однако, похоже, у меня проблемы с обработкой действий. Я получаю сообщение об ошибке:
Error: <[email protected]:add-team-modal::ember294> had no action handler for: hideModalКомпоненты тестирования элемента Ember Unit с действием пузырьков

Вот компонент modal.js:

stopSearch: function(modal) { 
    modal.send('hideModal'); 

    this.setProperties({ 
    searchTerm: '' 
    }); 
}, 

actions: { 
    modalCancelled: function(modal) { 
    this.stopSearch(modal); 
    }, 
    etc... 
} 

Как вы можете видеть, я кипящий вверх hideModal действие. Это тестовый модуль Я пытаюсь написать:

test('closing modal clears properties correctly', function(assert) { 
    assert.expect(2); 
    let component = this.subject(); 
    let firstSearchTerm; 

    Ember.run(function() { 
    component.set('searchTerm', 'test'); 
    firstSearchTerm = component.get('searchTerm'); 
    assert.ok(firstSearchTerm, 'test', 'set term properly'); 
    component.send('modalClosed', component); 
    }); 

    assert.ok(firstSearchTerm, '', 'clears term properly'); 
}) 

Прежде люди упомянуть this, я попытался его ниже.

test('closing modal clears properties correctly', function(assert) { 
    assert.expect(2); 
    let component = this.subject(); 

    let firstSearchTerm; 
    let $component = this.append(); 

    let targetObject = { 
    externalHideModal: function() { 
     assert.ok(true, 'hide modal was called.'); 
     return true; 
    } 
    } 

    component.set('hideModal', 'externalHideModal'); 
    component.set('targetObject', targetObject); 

    Ember.run(function() { 
    component.set('searchTerm', 'test'); 
    firstSearchTerm = component.get('searchTerm'); 
    component.send('modalCancelled', component); 
    }); 

    assert.ok(firstSearchTerm, '', 'clears term properly'); 
}) 

Выполнение попытки интеграции (не работает). Последнее утверждение все еще читает «тест».

test('Closing modal clears properties of modal', function(assert) { 
    assert.expect(1); 
    this.render(hbs`{{modal}}`); 

    //open modal 
    this.$('.search').click(); 

    this.setProperties({ 
    searchTerm: 'test' 
    }); 

    this.set('searchTerm', 'test'); 

    assert.equal(this.get('searchTerm'), 'test', 'sets properly'); 

    // cancel out of modal 
    this.$('.modal-footer button').eq(1).click(); 

    let searchTerm = this.get('searchTerm'); 

    assert.equal(searchTerm, '', 'clears results properly'); 
}); 
+0

Если у кого-нибудь есть идеи о том, как сделать эту интеграционную сторону, я мог бы также поэкспериментировать с этим. – atschaal

ответ

0

Если вы хотите, чтобы утверждать, что компонент отправки события вы можете добавить прослушиватель событий в тестовой интеграции контекста. Это также должно прекратить выброс необработанной ошибки.

assert.expect(2); 
... 
this.on('hideModal', function() { 
    assert.ok(true, 'hide modal event fired'); 
}); 

Это немного сложнее сделать с единичным угольком тестами, поэтому я бы рекомендовал использовать интеграционные тесты при тестировании взаимодействия компонентов пользовательского интерфейса.

Смежные вопросы