2015-07-10 14 views
5

ile başarısızlık React ile temel bir blog uygulaması yapıyorum. Ön uç testlerimi yürütmek için Jasmine ve Karma kullanıyorum. Benim ilk test kaldırmak ve çalışan var ve bu Chrome (Krom) ve Firefox'ta geçer, ancak aşağıdaki hatayı alıyorum PhantomJS çalıştırıldığında:Yasemin testleri Chrome ve Firefox'ta geçer, ancak PhantomJS

Benim sınama dosyası şuna benzer
PhantomJS 1.9.8 (Linux 0.0.0) ERROR 
    TypeError: 'undefined' is not a function (evaluating 'ReactElementValidator.createElement.bind(
      null, 
      type 
     )') 
    at /home/michael/repository/short-stories/test/karma_tests/story_test.js:1742 

:

var React = require('react/addons'); 
var Story = require('../../app/js/components/story.jsx'); 
var TestUtils = React.addons.TestUtils; 
var testUtilsAdditions = require('react-testutils-additions'); 

    describe('Story component', function() { 
    var component; 

    beforeEach(function() { 
     component = TestUtils.renderIntoDocument(React.createElement('story')); 
     component.props.storyTitle = 'front end test title'; 
     component.props.author = 'front end author'; 
     component.props.storyText = 'front end story text'; 
    }); 

    it('should display a story', function() { 
     expect(component.props).toBeDefined(); 
     expect(component.props.storyTitle).toBeDefined(); 
     expect(component.props.storyTitle).toBe('front end test title'); 
     expect(component.props.author).toBe('front end author'); 
     expect(component.props.storyText).toBe('front end story text') 
    }); 

    }); 

node_modules'ımı ve npm önbelleğini temizlemeyi ve npm yüklememi silmeyi denedim, ancak düzeltmedi. Testlerimin Firefox ve Chrome'da nasıl geçebileceğinden emin değilim, fakat PhantomJS'de değil. Projenin tamamını burada görebilirsiniz: https://github.com/mrbgit/short-stories. Yardımcı olabilecek daha fazla bilgi varsa bana bildirin. Herhangi bir yardım takdir edilir. Teşekkürler!

cevap

9

PhantomJS, Function.prototype.bind sağlamayan oldukça eski bir Qt-Webkit sürümünü kullanır. Bu, birçok kitaplık için bir sorun olduğundan, polyfill NPM module called 'phantomjs-polyfill' kullanılabilir.

NPM modülleri kullanmayı tercih etmiyorsanız (browserify/webpack ile birlikte verilmemiş bir tarayıcı sitesini test ediyorsanız), MDN sayfasında bind için aşağıdaki polyfill sağlanmıştır ve bunu ekleyebilirsiniz. kendiniz:

if (!Function.prototype.bind) { 
    Function.prototype.bind = function(oThis) { 
    if (typeof this !== 'function') { 
     // closest thing possible to the ECMAScript 5 
     // internal IsCallable function 
     throw new TypeError('Function.prototype.bind - what is trying to be bound is not callable'); 
    } 

    var aArgs = Array.prototype.slice.call(arguments, 1), 
     fToBind = this, 
     fNOP = function() {}, 
     fBound = function() { 
      return fToBind.apply(this instanceof fNOP 
       ? this 
       : oThis, 
       aArgs.concat(Array.prototype.slice.call(arguments))); 
     }; 

    fNOP.prototype = this.prototype; 
    fBound.prototype = new fNOP(); 

    return fBound; 
    }; 
} 
+0

Teşekkürler ssube, bu düzeltildi! – CascadiaJS

İlgili konular