简体   繁体   English

reactjs在使用Jasmine进行测试时获取``未定义不是函数''

[英]reactjs get `undefined is not a function` on testing with Jasmine

I have a React components. 我有一个React组件。 I want to test them. 我想测试一下。 But I get unexpected error on every trying to findRenderedDOMComponentWithTag with tested component. 但是,每次尝试使用已测试的组件来findRenderedDOMComponentWithTag ,我都会遇到意外错误。 Here is the Karma error log: 这是Karma错误日志:

05 09 2015 20:31:23.450:INFO [watcher]: Changed file "/tmp/35ffb917aab483a567d1be6fed779291.browserify".
PhantomJS 2.0.0 (Linux 0.0.0) DestroySession should process user logout FAILED
    TypeError: undefined is not a function (evaluating 'target.dispatchEvent(e)') in http://localhost:9876/karma.js (line 1134)
        at /tmp/35ffb917aab483a567d1be6fed779291.browserify:59730:16
PhantomJS 2.0.0 (Linux 0.0.0): Executed 3 of 7 (1 FAILED) (skipped 4) (0.04 secs / 0.019 secs)

My stack is: 我的堆栈是:

  • coffeescript CoffeeScript的
  • react(+jsx) 反应(+ JSX)
  • browserify browserify
  • karma.js karma.js
  • phantomjs phantomjs
  • jasmine 茉莉

Component: 零件:

React = require('react')
ReactBootstrap = require('react-bootstrap')

Button = ReactBootstrap.Button

SessionActions = require('../../actions/session_actions.coffee')

module.exports = React.createClass
  contextTypes: router: React.PropTypes.func
  handleClick: (e) ->
    e.preventDefault()
    console.log 'хуйло'
    SessionActions.destroy()
    @context.router.transitionTo('/sessions/new')
  render: ->
    <Button onClick={@handleClick} className='btn btn-default navbar-btn'>Sign out</Button>

Test: 测试:

React = require('react/react-with-addons.js')
TestUtils = React.addons.TestUtils

DestroySession = require('../../../../app/coffee/components/sessions/destroy.coffee')

describe 'DestroySession', ->
  instance = undefined

  beforeEach ->
    instance = TestUtils.renderIntoDocument(<DestroySession />)


  it 'should process user logout', ->
    localStorage.setItem('token', 123)
    localStorage.setItem('userName', 'Anonymous Person')
    localStorage.setItem('userId', 11)

    button = TestUtils.findRenderedDOMComponentWithTag(instance, 'button')

    console.log button

It's very strange, but I get an error when I try to console.log my button variable. 这很奇怪,但是当我尝试console.log我的button变量时出现错误。 If I comment out last line of my test, it will be passed. 如果我注释掉测试的最后一行,它将通过。 What's happened? 发生了什么?

you need to call getDOMNode() 您需要调用getDOMNode()

console.log button.getDOMNode()

Note : This is changing in 0.14.2 where TestUtils.find/scry methods will return the DOM node directly. 注意 :这在0.14.2中进行了更改,其中TestUtils.find / scry方法将直接返回DOM节点。

This is probably not the right answer, but I landed here because I got the exact same exception in Jasmine, and I uncovered what caused it, so I'll leave this here: 这可能不是正确的答案,但由于我在茉莉花中得到了完全相同的异常,所以我登陆了这里,并且我发现了导致它的原因,所以我将其留在这里:

The mistake I made was to accidentally save the global document inside an object, instead of a local variable called document : 我犯的错误是不小心将全局文档保存在对象内部,而不是将局部变量保存为document

/*
var document = {
   parentId: parentItem._id, 
   childIds: [childItem._id],
};
I had moved the above elsewhere, so "document" now refers to window.document
*/
db.saveInIndex(parentId, document);

And later made a call to this: 然后打电话给这个:

console.log(db.getFromIndex(parentId));

Which resulted in this: 结果是这样的:

TypeError: undefined is not a function (evaluating 'target.dispatchEvent(e)')

Because it was trying to log an object that looked like this: 因为它试图记录看起来像这样的对象:

{
  parentId: 'HA89A8S98A98',
  document: <<< the global "document" - not what we wanted! >>>
}

That global var document would normally be the HTML contents of your page, but when you're running your tests against PhantomJS you get it's own interpretation of that! 该全局var 文档通常是页面的HTML内容,但是当您针对PhantomJS运行测试时,您会得到它的解释!

(running Karma against Chrome gave a different exception) (针对Chrome浏览器运行Karma的情况有所不同)

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

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