2017-05-22 4 views
1

테스트 도중 React 애플리케이션에서 내 데이터 레이어를 전환 할 수 있기를 원합니다. 각 세계에서이 ...React에서 종속성을 어떻게 관리합니까?

class Repository{ 
    getData(){ 
    //use http and get some data 
    } 
} 

class AngularController(Repository){ 
    var self = this; 
    Repository.getData().then((objects) => { 
     this.pageObjects = objects; 
    }) 
} 

//test 
inject('check that controller sets data',(Repository, $controller) => { 
    spyOn(Repository.getData).returns(fakeData); 
    var controller = $controller('AngularController'); 
    expect(controller.data).toBe(fakeData); 
}) 

그러나 명백하게가 작동하고 IOC 나쁜 내가 대신 IOC의 모듈을 사용하여, 그래서 '해야한다'내가 사용하는 세계를 반응하는 내가 이런 짓을 했을까 쉬웠다 나는 어떻게 이것을 React에서 할 수 있을까? 내가 그들을 스텁 수

답변

0
당신은 스텁 sinon을 사용할 수 있습니다

import * as repo from 'repository'; 

모든 수출 기능을 가진 객체를 얻을 수 있습니다.

import sinon from 'sinon'; 
import ReactComponent from 'ReactComponent'; 
import * as repo from 'Repository'; 

sinon.stub(repo, 'getData', m => { 
    return ["some", "data"]; 
}); 

describe('component stuff',() => { 
    it('component fetching phase',() => { 
    //not sure how you test your component rendering... 
    //but you can use jest or enzyme 
    }); 
}); 
관련 문제