2014-05-08 50 views
0

这是一个$资源的测试与装载机测试的角度JS装载机

describe('Service: MultiCalculationsLoader', function(){ 

    beforeEach(module('clientApp')); 

    var MultiCalculationLoader, 
    mockBackend, 
    calculation; 

    beforeEach(inject(function (_$httpBackend_, Calculation, _MultiCalculationLoader_) { 
    MultiCalculationLoader = _MultiCalculationLoader_; 
    mockBackend = _$httpBackend_; 
    calculation = Calculation; 
    })); 

    it('should load a list of calculation from a user', function(){ 
    mockBackend.expectGET('/api/user/600/calculation').respond([{id:5}]); 

    var calculations; 
    var mockStateParams = { 
     userId: 600 
    }; 
    var promise = new MultiCalculationLoader(mockStateParams); 

    promise.then(function(res){ 
     calculations = res 
    }); 

    expect(calculations).toBeUndefined(); 

    mockBackend.flush(); 

    expect(calculations).toEqual([{id:5}]); 
    }); 

}); 

当我运行测试,我得到以下错误:

Expected [ { id : 5 } ] to equal [ { id : 5 } ]. 
Error: Expected [ { id : 5 } ] to equal [ { id : 5 } ]. 
    at null.<anonymous> 

我不明白它。这两个阵列对我来说是一样的。想法任何人?

更新 这里的实现:

.factory('Calculation', function ($resource) { 
    return $resource('/api/user/:userId/calculation/:calcId', {'calcId': '@calcId'}); 
    }) 
    .factory('MultiCalculationLoader', function (Calculation, $q) { 
    return function ($stateParams) { 
     var delay = $q.defer(); 
     Calculation.query({userId: $stateParams.userId},function (calcs) { 
     delay.resolve(calcs); 
     }, function() { 
     delay.reject('Unable to fetch calculations'); 
     }); 
     return delay.promise; 
    }; 
    }) 
+0

它似乎工作:http://plnkr.co/edit/DLUaEEzBnyWOUudsaooG?p=preview –

+0

@KhanhTO Yeha但它不工作在我的开发环境。 – Per

+0

你能展示你的'MultiCalculationLoader'是如何实现的吗? –

回答

2

的网址你的期望是不同的来自实际的网址。我想你需要的东西是这样的:

it('should load a list of calculation from a user', function(){ 
    //remove the 's' from 'calculations' 
    mockBackend.expectGET('/api/user/600/calculation').respond([{id:5}]); 

    var calculations; 
    var promise = MultiCalculationLoader({userId:600}); //userId = 600 
    promise.then(function(res){ 
     calculations = res 
    }); 

    expect(calculations).toBeUndefined(); 

    mockBackend.flush(); 

    expect(calculations).toEqual([{id:5}]); 
    }); 

还有就是角自动添加两个属性到响应另一个问题:

enter image description here

http://plnkr.co/edit/gIHolGd85SLswzv5VZ1E?p=preview

这是一种相同的问题的:AngularJS + Jasmine: Comparing objects

这实际上是一个问题,角$资源w hen将角色转换为资源对象的响应。为了验证从$资源的响应,你可以尝试angular.equals

expect(angular.equals(calculations,[{id:5},{id:6}])).toBe(true); 

http://plnkr.co/edit/PrZhk2hkvER2XTBIW7yv?p=preview

你也可以写一个自定义的匹配:

beforeEach(function() { 
    jasmine.addMatchers({ 
     toEqualData: function() { 
     return { 
      compare: function(actual, expected) { 
      return { 
       pass: angular.equals(actual, expected) 
      }; 
      } 
     }; 
     } 
    }); 
    }); 

并使用它:

expect(calculations).toEqualData([{id:5},{id:6}]); 

http://plnkr.co/edit/vNfRmc6R1G69kg0DyjZf?p=preview

+0

你有任何解决方案如何实际解决它? – Per

+0

@ user1572526:查看我更新的答案。 –

+0

这是不是有点混乱?也许我完全在这里,但如何使用angular.toJson?我认为从数组中删除“垃圾”? – Per

0

茉莉花平等选择可太specfic有时当你equaity只是想检查。

在比较对象或数组时,我从来没有用toEqual()方法看过它,但使用toBe()方法定义def。

尝试用toMatch()替换toEqual()。

另外在单元测试中,我建议使用一个常数值,您可以在响应和匹配器/相等/ toBe中传递。

describe('Service: MultiCalculationsLoader', function(){ 

    beforeEach(module('clientApp')); 

    var MultiCalculationLoader, 
    mockBackend, 
    calculation, 
    VALUE = [{id:5}]; 

    beforeEach(inject(function (_$httpBackend_, Calculation, _MultiCalculationLoader_) { 
    MultiCalculationLoader = _MultiCalculationLoader_; 
    mockBackend = _$httpBackend_; 
    calculation = Calculation; 
    })); 

    it('should load a list of calculation from a user', function(){ 
    mockBackend.expectGET('/api/user/600/calculations').respond(VALUE); 

    var calculations; 
    var promise = MultiCalculationLoader(); 
    promise.then(function(res){ 
     calculations = res 
    }); 

    expect(calculations).toBeUndefined(); 

    mockBackend.flush(); 

    expect(calculations).toEqual(VALUE); 
    }); 

}); 

使用这种方法,我认为.toEqual将实际工作。

应对方法:

块之前:

httpBackend.when('JSONP', PATH.url + 'products?callback=JSON_CALLBACK&category=123').respond(CATEGORIES[0]); 

测试:

describe('Category Method', function() { 

     it('Should return the first category when the method category is called', function() { 
      var result = ''; 

      service.category(123).then(function(response) { 
       result = response; 
      }); 

      httpBackend.flush(); 
      expect(result).toEqual(CATEGORIES[0]); 

     }); 
    }); 
+0

没有以常量值工作。但它与toMatch()一起工作。谢谢! – Per

+0

嗯,很奇怪 - 它是我们在单元测试中使用的确切方法......尝试返回VALUE [0]并测试它是否相等。 –

+0

也许与expectGET vs方法有关,以及它们如何返回数据.... –

0

你可以试着改变toEqual()与toEqualData()

expect(calculations).toEqualData([{id:5}]); 
+0

找不到“toEqualData”函数? – Per