1
我正在嘗試爲我的響應組件編寫單元測試。這裏是我的組件:聯合測試響應組件無法識別內置函數
import React from "react";
require("../../../../css/ads/ads.css");
export class Ads extends React.Component {
render() {
return (
<div className="ads-container txt-flex-alignment">
<a id={"dummyAdsLink" + this.props.channel.removeAllSpaces().toLowerCase() + this.props.adsId}
href="#"
target="_top"><img
id={"dummyAdsImg" + this.props.channel.removeAllSpaces().toLowerCase() + this.props.adsId}
className="img-responsive" src={this.url}
/></a>
</div>
);
}
}
在上面的代碼removeAllSpace是我由JavaScript原型的擴展創建如下功能:
String.prototype.removeAllSpaces = function() {
return this.replace(/\s+/g, '');
}
這裏是我的測試:
import React from 'react';
import expect from 'expect.js';
import {shallow} from 'enzyme';
import {Ads} from "../../src/app/components/story/ads/Ads";
import renderer from 'react-test-renderer';
describe('<Ads />',() => {
it('should render 1 <a /> components',() => {
const wrapper = shallow(<Ads channel="whatevername"/>);
expect(wrapper.find('a')).to.have.length(1);
});
});
現在當我運行測試時,出現以下錯誤:
TypeError: this.props.channel.removeAllSpaces is not a function
顯然它不喜歡我的removeAllSpaces函數..任何想法?
更新:當我刪除removeAllSpaces運作一切正常