피드를 가져 오기 위해 Api라는 맞춤 API 서비스 클래스에 의존하는 피드를 사용자에게 표시하기위한이 aurelia 구성 요소가 있습니다. Api 클래스에는 데이터를 가져 오기 위해 HttpClient를 사용하는 get() 함수가 있습니다.Aurelia inject mock dependency
구성 요소를 테스트하려고합니다. 서비스 클래스, 특히 get 함수를 조롱하여 적절한 테스트 데이터를 반환하고이 모의 객체를 aurelia의 DI 컨테이너를 통해 구성 요소에 주입하려고합니다. DI 부분에 문제가 있습니다. 여기
내 시험에서 구성 요소의 JS 파일import {bindable, inject} from 'aurelia-framework';
import {Api} from 'services/api';
@inject(Api)
export class Feed {
events = null;
constructor(api) {
console.info('feed.js constructor, api:', api)
this.api = api;
}
의 관련 부분과 관련 코드
beforeEach(done => {
...
let mockApi = new Api();
spyOn(mockApi, 'get').and.returnValue(mockGetResponse);
const customConfig = (aurelia) => {
let conf = aurelia.use.standardConfiguration().instance("Api", mockApi);
console.info('Registering Api:', conf.container.get("Api"));
return conf;
}
const ct = new ComponentTester();
ct.configure = customConfig;
sut = ct.withResources('activityfeed/feed');
sut.inView('<feed username.bind="username"></feed>')
.boundTo({username: TEST_USER});
sut.create(bootstrap).then(() => {
done();
});
});
이 코드는 내가 말할 수있는 한 실제로 의도 된 방식으로 작동합니다. 구성 요소 생성시 customConfig 함수가 호출되고 mockApi 인스턴스가 콘솔에 기록됩니다.
그러나 나중에 부트 스트랩 프로세스에서 구성 요소 생성자는 여전히 컨테이너에 등록 된 내 모의 인스턴스 대신 실제 Api 서비스 클래스의 인스턴스를받습니다.
아무런 도움없이 도움을받을 수 있다면 매우 감사하게 생각합니다.
또는이를 수행 할 수있는 대체 방법이있는 경우에도 마찬가지입니다.
답변 해 주셔서 감사합니다. 세련된 후에 나는 당신이'aurelia.container.registerInstance (Api, api); 대신'aurelia.use.standardConfiguration(). instance (Api, mockApi); '를 사용하는 것과 비슷한 코드를 작성했다. ' AFAICT는 똑같은 일을합니다. 차이가 있는지 확인하십시오. –
FrameworkConfiguration 클래스의 소스 코드를 보면 인스턴스는 Container.registerInstance를 호출하므로 차이가 없습니다.둘 다 API 설명서에 설명되어 있습니다. – martin