2013-07-26 6 views
8

Toran Billup의 TDD guide을 사용하여 ember와의 통합 테스트에 문제가 있습니다.경로 전환에 Ember 통합 테스트를 수행하는 방법은 무엇입니까?

Qunit 및 Phantom JS의 테스트 주자로 Karma를 사용하고 있습니다.

Ember runloop에 대한 초보자의 지식과 관련이 있다면 절반이라고 확신합니다. 내 질문은 두 부분입니다 :

1) 어떻게 실행 루프에 적절하게 vist() 테스트를 래핑합니까?

2) 전환을 테스트하려면 어떻게해야합니까? 인덱스 경로 ('/')는 'projects.index'라는 리소스 경로로 전환해야합니다.

module("Projects Integration Test:", { 
    setup: function() { 
    Ember.run(App, App.advanceReadiness); 
    }, 
    teardown: function() { 
    App.reset(); 
    } 
}); 

test('Index Route Page', function(){ 
    expect(1); 
    App.reset();  
     visit("/").then(function(){ 
      ok(exists("*"), "Found HTML"); 
     }); 
}); 

올바른 방향의 포인터에 대해 미리 감사드립니다. (당신이 언급 한대로)

Ember.run.next(function(){ 
    //do somethin 
    transition stuff here etc 
}); 

은 당신이 할 수있는 현재의 경로를 확인하려면

+0

어떤 버전의 ember를 사용하고 있습니까? 나는 당신이 "/"경로를 방문 할 때 깨지는 RC 6의 치명적인 버그에 대해서는 언급하지 않았다. https://github.com/emberjs/ember.js/issues/2997 –

+0

Ah! 나는 R6.1을 사용하고있다! 미안하지만, 내 질문에 언급 깜빡. 도와 줘서 고마워! – ganicus

답변

7

에 대한 몇 가지 정보를 썼습니다

https://github.com/toranb/ember-testing-example

RC5

는 간단한 "안녕하세요 세계"의 예는 테)이

1. 같습니다 당신은 전환

<table> 
{{#each person in controller}} 
<tr> 
    <td class="name">{{person.fullName}}</td> 
    <td><input type="submit" class="delete" value="delete" {{action deletePerson person}} /></td> 
</tr> 
{{/each}} 
</table> 

2)를 ember.js 응용 프로그램 코드

App = Ember.Application.create(); 

App.Router.map(function() { 
    this.resource("other", { path: "/" }); 
    this.resource("people", { path: "/people" }); 
}); 

App.OtherRoute = Ember.Route.extend({ 
    redirect: function() { 
     this.transitionTo('people'); 
    } 
}); 

App.PeopleRoute = Ember.Route.extend({ 
    model: function() { 
     return App.Person.find(); 
    } 
}); 

App.Person = Ember.Object.extend({ 
    firstName: '', 
    lastName: '' 
}); 

App.Person.reopenClass({ 
    people: [], 
    find: function() { 
     var self = this; 
     $.getJSON('/api/people', function(response) { 
      response.forEach(function(hash) { 
       var person = App.Person.create(hash); 
       Ember.run(self.people, self.people.pushObject, person); 
      }); 
     }, this); 
     return this.people; 
    } 
}); 

3) 통합 시험이

module('integration tests', { 
    setup: function() { 
     App.reset(); 
     App.Person.people = []; 
    }, 
    teardown: function() { 
     $.mockjaxClear(); 
    } 
}); 

test('ajax response with 2 people yields table with 2 rows', function() { 
    var json = [{firstName: "x", lastName: "y"}, {firstName: "h", lastName: "z"}]; 
    stubEndpointForHttpRequest('/api/people', json); 
    visit("/").then(function() { 
     var rows = find("table tr").length; 
     equal(rows, 2, rows); 
    }); 
}); 

4)처럼 보이는 동안으로 리디렉션 mplate 대부분의 내 ember.js 프로젝트에서 사용하는 통합 도우미

document.write('<div id="foo"><div id="ember-testing"></div></div>'); 

Ember.testing = true; 

App.rootElement = '#ember-testing'; 
App.setupForTesting(); 
App.injectTestHelpers(); 

function exists(selector) { 
    return !!find(selector).length; 
} 

function stubEndpointForHttpRequest(url, json) { 
    $.mockjax({ 
     url: url, 
     dataType: 'json', 
     responseText: json 
    }); 
} 

$.mockjaxSettings.logging = false; 
$.mockjaxSettings.responseTime = 0; 
4

나는 카르마에 익숙 해요,하지만 엠버와 상호 작용해야하는 테스트의 부분은 실행 루프에 푸시한다 꺼내서 정보를 훔쳐 내면, 어떤 점에서 스택 오버플로에서 훔친 정보가 있습니다. 당신이 컨트롤러 테스트를하고 시작하는 경우

var router = App.__container__.lookup("router:main"); //get the main router 
var currentHandlerInfos = router.router.currentHandlerInfos; //get all handlers 
var activeHandler = currentHandlerInfos[currentHandlerInfos.length - 1]; // get active handler 
var activeRoute = activeHandler.handler; // active route 

은, 난 그냥 당신이 "/"경로를 사용하여 ember.js이 명중 할 때 간단한 전환을 수행하는 예제 응용 프로그램을 추진하는 http://discuss.emberjs.com/t/unit-testing-multiple-controllers-in-emberjs/1865

+0

Ember.run.next()가 내 테스트를 오류없이 실행했으나 어설 션이 없다는 Fail 메시지가 나타납니다. 위의 예제는 핸들러에 대한 최신 정보를 얻기 위해 현재 경로를 두드리는 것에 대해 좋아합니다. 나는 내 테스트에서 그걸 가지고 놀 것입니다! – ganicus

관련 문제