2017-12-12 11 views
0

테스트 환경으로 카르마/모카를 사용하고 있습니다. url을 얻으려면 window.location.href에 의존하는 js 파일이 있습니다. Karma를 사용하여 테스트를 수행 할 때 기본 URL은 www.localhost : 3123/context.html입니다. url/add 매개 변수를 변경하거나이 특정 테스트 스위트에 대한 사용자 정의 URL을 사용하도록 karma에 말할 수 있습니까?모카/카르마 스택의 window.location.href 변경

//JS file has 
function populate(){ 
    var url = new URL(window.location.href); 
    -- check if the url have parameter, lets say carInfo 
    -- if has then dispatches an event 
} 

//Mocha test 
describe('...', function() { 
    it ('...', function() { 
     -- call populate() 
     -- listen if the event was triggered 
    }) 
}) 

답변

1

일반적으로 코드에서 종속성을 분리하면 테스트에서 이러한 종속성을 교환 할 수 있습니다. 그 배경 개념은 반전 제어입니다. 당신은 또한 의존성 주입을 고려해 볼 수 있습니다. 당신의 콘크리트의 경우 window.location.href에서

는 Node.js를에서 테스트 또는 뭔가를 실행할 때 존재하지 않는 특별한 환경, 브라우저에 특정 종속성입니다

function populate(url){ 
    // trigger the event... 
} 

describe('...', function() { 
    it ('...', function() { 
     populate(new URL("http://...") 
     // listen if the event was triggered 
    }) 

})