1
나는 ajax 호출 ($ http)을 랩핑하고 약속을 되 돌리는 서비스를 가지고있다. 내 컨트롤러에서 나는 $scope.stuff
의 값을 테스트하려면 어떻게 MyService.stuff(…).
then
(fn(data){ $scope.stuff = data; })
재스민 : 약속 함수 테스트
과의 약속을 해결? 나는 시험에있어 무엇
// controller
$scope.stuff = false;
$scope.getStuff = function(name)
{
if (!angular.isDefined($scope.users[ name ]))
MyService.stuff('get' , username).then(function(data)
{ // returns the value of resp.data
$scope.stuff = $scope.users[ name ] = data;
});
else
$scope.stuff = $scope.users[ name ];
};
은 다음과 같습니다 : 테스트 사이클을 소화 2 필요 마치
// test
describe('scope.stuff',function()
{
it('should initialize as false', function()
{ // this passes
expect(scope.stuff).toBe(false);
});
var httpBackend;
beforeEach(inject(function($httpBackend){
httpBackend = $httpBackend;
}));
it('should have 4 elements', function()
{ // this stalls
runs(function()
{
httpBackend.whenGET().respond(200,
{ data: [
{name: 'foo'}, {name: 'bar'}, {name: 'baz'},{name: 'qux'}
]});
scope.getStuff('foo');
});
waitsFor(function()
{
scope.$apply();
return scope.stuff;
});
runs(function()
{
expect(scope.stuff.length).toBe(4);
});
});
});
테스트에 2 다이제스트주기가 필요합니다. 서버 응답 및 범위 시뮬레이션을위한 httpBackend.flush() 약속을 해결하기위한 $ apply() –
@EitanPeer, 지연 문제가 해결 된 것 같습니다! 그러나 * MyService *는 httpBackend의 응답을 가로 채지 않는 것처럼 보입니다. expect의 바로 위에 'scope.stuff'를 기록하면 * data * 값 대신에 전체 객체를 얻습니다. 왜 그런지 아십니까? – jacob
@EitanPeer, 신경 쓰지 마, 나는 바보 야. 응답 객체의 * data * obj에 넣는 * data *라는 객체로 페이로드를 제공했습니다. 당신의 도움을 주셔서 감사합니다! 답장으로 의견을 게시하여 승인하고 질문을 닫을 수 있습니까? – jacob