【发布时间】:2015-10-21 15:28:09
【问题描述】:
我有我的test.login.js:
it('calls login when there\'s a username present', () => {
React.findDOMNode(LoginElement.refs.username).value = 'foo';
TestUtils.Simulate.submit(form);
expect(LoginElement.state.errored).toEqual(false);
});
通过提交表单,它调用了一个登录方法:
login() {
let typedUsername = React.findDOMNode(this.refs.username).value;
if (!typedUsername) {
return this.setState({
errored: true
});
}
// we don't actually send the request from here, but set the username on the AuthModel and call the `login` method below
AuthModel.set('username', typedUsername);
AuthModel.login();
},
所以我正在尝试测试Login.jsx 的功能,而不是AuthModel.js,但是通过调用AuthModel.login(),它会通过WebSocket 发送一条消息。但是,问题是在我的实际应用程序中,在 WebSocket 连接之前我不会加载任何东西(我触发一个事件然后呈现 React 应用程序),但是在我的 Jasmine 测试中,我不等待这个事件,所以我收到:
ERROR: null, DOMException{stack: 'Error: Failed to execute 'send' on 'WebSocket': Still in CONNECTING state.
我的测试失败了,它不应该失败,因为它封装的功能可以满足我的要求。它只是在依赖关系树上进一步出错。
解决此问题或减轻 WebSocket 尝试在我的测试环境中连接的最佳方法是什么? (我对测试非常陌生,所以这些概念现在对我来说非常陌生)
【问题讨论】:
标签: javascript unit-testing websocket jasmine reactjs