2016-08-08 128 views
0

我有一个REST客户端被写入我想要编写测试的angularJS应用程序的一部分;我尝试过使用Jasmine(在$ httpBackend上使用passthrough),但根本无法让它与真正的端点进行交谈(这是一项要求)。

有没有人知道一个合理的库,允许这个?或者,茉莉花摔跤的方式?

+0

你可以用量角器或别的东西 – user3232739

+0

端到端测试测试它为什么如果你不使用摩卡只是想测试后端休息API – MarkoCen

+1

如果你想测试从客户端到服务器的真实http请求,那么你不需要使用$ httpBackend。我已经在茉莉花写了测试,要求真正的端点。 Jasmine支持异步测试,因此编写与真正的rest API进行通信的测试不成问题。 – thadam

回答

0

你需要注入第一$ httpbackend

describe('MyController', function() { 
var $httpBackend, $rootScope, createController, authRequestHandler; 

// Set up the module 
    beforeEach(module('MyApp')); 

    beforeEach(inject(function($injector) { 
// Set up the mock http service responses 
$httpBackend = $injector.get('$httpBackend'); 
// backend definition common for all tests 
authRequestHandler = $httpBackend.when('GET', '/auth.py') 
         .respond({userId: 'userX'}, {'A-Token': 'xxx'}); 

// Get hold of a scope (i.e. the root scope) 
$rootScope = $injector.get('$rootScope'); 
// The $controller service is used to create instances of controllers 
var $controller = $injector.get('$controller'); 

createController = function() { 
    return $controller('MyController', {'$scope' : $rootScope }); 
}; 
    $httpBackend.when('GET', "/api/rest/").respond(data_to_respond); 
    })); 

下一个写测试用例

it('getTypes - should return 3 car manufacturers', function() { 
     service.getTypes().then(function(response) { 
      //expect will be here 
     }); 
     $httpBackend.flush(); 
    }); 
+0

正如我在我的问题中所说的,我不想虚假回应 - 我希望我的客户与真实的服务器交谈。 – Fallso

+0

当测试你不得不伪造回应 –

+0

因此我问是否有其他测试框架,你不需要,因为我确定这是茉莉花的限制:) – Fallso