1

它看起来很简单,但无论我使用$httpBackend与否,它永远不会返回!

    describe('R', function(){
      var Res, http, item, original = {name:"John"};
      beforeEach(function () {
        module('app');
        inject(function ($httpBackend, $resource) {
            http = $httpBackend;
            Res = $resource('/api/user/:user', {user:'@id'});
        });
      });
      beforeEach(function (done) {
        http.when('GET','/api/user/1').respond(original);
        console.log("I GET CALLED");
        Res.get({id:'1'},function (res) {
            console.log("I NEVER GET CALLED");
            item = res;
            done();
        });
      });
      it('should just be true', function(){
        expect(true).toBe(true);
      });
});

为什么第二console.log永远不会被调用?

4

1 回答 1

1

我知道了。花费了无数个小时来解决这个问题(好吧,很多时间问题,其中 jsfiddle 有时会给出一个“未声明的变量描述”错误,这让我偏离了方向)。简而言之:

    http.when('GET','/api/user/1').respond(original);
    console.log("I GET CALLED");
    Res.get({id:'1'},function (res) {
        console.log("I NEVER GET CALLED");
        item = res;
        done();
    });
    http.flush();

叹...

于 2014-10-07T13:17:13.937 回答