试图用茉莉花反应测试React组件

时间:2015-06-19 11:02:24

标签: jasmine reactjs stub

这个问题与one of the solutions to this question非常相似。

我用这个作为我的例子,因为它类似于我的真实问题,但很好地提炼了。

以下测试产生此结果:

Warning: Comp.type is deprecated. Use Comp directly to access the class.

 Error: Expected a spy, but got undefined.

测试:

it("should call plop method on render", function(){

      var Comp = React.createClass({

        displayName: "Comp",

        plop: function() {
          console.log("plop");
        },

        render: function() {
          this.plop();
          return (<div></div>);
        }
      });


      //spy on method
      jasmineReact.spyOnClass(Comp, "plop");
      jasmineReact.render(<Comp />);
      expect(Comp.plop).toHaveBeenCalled();
    })

知道我做错了吗?

1 个答案:

答案 0 :(得分:0)

JasmineReact的方法期望语法与您使用它的方式不同。试试这个:


var React = require('react-native/addons');

此外,如果您对状态等操作有所期望,则需要使用expect(jasmineReact.classPrototype(Comp).plop).toHaveBeenCalled(); 返回的实例,而不是类:

render