7

存根 $httpBackend 后,我似乎无法返回预期的结果,也不知道测试出了什么问题。你能看看这个失败的最小例子吗?

与演示链接

http://jsfiddle.net/paos/kTNF5/

茉莉花输出

query() Expected [ ] to equal [ { hello : 'world' } ].

代码

依赖资源
<script src="https://raw.github.com/pivotal/jasmine/master/lib/jasmine-core/jasmine.js"></script>
<script src="https://raw.github.com/pivotal/jasmine/master/lib/jasmine-core/jasmine-html.js"></script>

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular-resource.min.js"></script>
<script src="http://code.angularjs.org/1.0.1/angular-mocks-1.0.1.js"></script>
测试 javascript
var mod = angular.module('mod', ['ngResource']);                                                                                                                               

mod.factory('Brief', function($resource) {
   var Brief = $resource('http://some_test/:id');                                                                                                                              
   return Brief;                                                                                                                                                               
});                                                                                                                                                                            

beforeEach(function() {
    module('mod');                                                                                                                                                             

    inject(function($injector) {
        resource = $injector.get('$resource');                                                                                                                                 
        $httpBackend = $injector.get('$httpBackend');                                                                                                                          
        Brief = $injector.get('Brief');                                                                                                                                        
    });                                                                                                                                                                        

});                                                                                                                                                                            

afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation();                                                                                                                             
    $httpBackend.verifyNoOutstandingRequest();                                                                                                                                 
});                                                                                                                                                                            

describe("Brief", function() {
    it("query()", function() {
        var getRequest = 'http://some_test';                                                                                                                                   
        var fakeGetResponse = [{
            "hello": "world"}];                                                                                                                                                

        $httpBackend.when('GET', getRequest).respond(fakeGetResponse);                                                                                                         

        var briefs = Brief.query();                                                                                                                                            
        expect(briefs).toEqual(fakeGetResponse);                                                                                                                               
        $httpBackend.flush();                                                                                                                                                  
    });                                                                                                                                                                        
});                                                                                                                                                                            


// KICK OFF JASMINE
var jasmineEnv = jasmine.getEnv();                                                                                                                                             
var trivialReporter = new jasmine.TrivialReporter();                                                                                                                           

jasmineEnv.addReporter(trivialReporter);                                                                                                                                       

jasmineEnv.specFilter = function(spec) {
    return trivialReporter.specFilter(spec);                                                                                                                                   
};                                                                                                                                                                             

jasmineEnv.execute();​               

谢谢!

4

1 回答 1

12

您已经快到了,但您的测试中仍然存在 2 个问题:

  • $httpBackend在刷新之前验证了期望。它是$httpBackend.flush();模拟传入响应的
  • 在您的期望中,您正在比较对象实例(而不是它们的值),但 AngularJS$resource将创建一个新数组,如https://stackoverflow.com/a/11966512/1418796中所述。您需要比较对象的值而不是引用。这样做的一种方法是:expect(angular.equals(briefs, fakeGetResponse)).toBeTruthy();

最后这里是一个测试通过的 jsFiddle:http: //jsfiddle.net/VFqFU/

于 2012-12-02T09:29:34.000 回答