2016-12-08 6 views
0

"Rails、Angular、Postgres、and Bootstrap"(114ページ)の本を読んでいて、私が問題に遭遇しました。Rails、Angularjs、unit test:予期しない要求:GET

ここでは、レールアプリケーションでangularjsをテストする方法について説明します。ここに私のコードは次のとおりです。

describe("CustomerSearchController", function() { 
    describe("Initialization", function() { 
     var scope = null, 
     controller = null; 
     beforeEach(module("customers")); 
     beforeEach(inject(function ($controller, $rootScope) { 
      scope = $rootScope.$new(); 
      controller = $controller("CustomerSearchController", { 
       $scope: scope 
      }); 
     })); 

     it("defaults to an empty customer list", function() { 
      expect(scope.customers).toEqualData([]); 
     }); 

    }); 

    // Problematic code here: 
    describe("Fetching Search Results", function() { 
     beforeEach(module("customers")); 


     var scope = null, 
      controller = null, 
      httpBackend = null, 
      serverResults = [ 
       { 
        id: 123, 
        first_name: "Bob", 
        last_name: "Jones", 
        email: "[email protected]", 
        username: "jonesy" 
       }, 
       { 
        id: 456, 
        first_name: "Bob", 
        last_name: "Johnsons", 
        email: "[email protected]", 
        username: "bobbyj" 
       } 
      ]; 

     beforeEach(inject(function ($controller, $rootScope, $httpBackend) { 
      scope = $rootScope.$new(); 
      httpBackend = $httpBackend; 
      controller = $controller("CustomerSearchController", { 
       $scope: scope 
      }); 
     })); 


     it("populates the customer list with the results", function() { 
      scope.search("bob"); 
      httpBackend.flush(); 
      expect(scope.customers).toEqualData(serverResults); 
     }); 
    }); 
    }); 

ここに私が取得エラーです:

$ bundle exec rake teaspoon 
Starting the Teaspoon server... 
Puma starting in single mode... 
* Version 3.6.2 (ruby 2.3.1-p112), codename: Sleepy Sunday Serenity 
* Min threads: 5, max threads: 5 
* Environment: test 
* Listening on tcp://127.0.0.1:41239 
Use Ctrl-C to stop 
Teaspoon running default suite at http://127.0.0.1:41239/teaspoon/default 
.F. 

Failures: 

    1) CustomerSearchController Fetching Search Results populates the customer list with the results 
    Failure/Error: Error: Unexpected request: GET /customers.json?keywords=bob&page=0 
No more request expected in http://127.0.0.1:41239/assets/angular-mocks/angular-mocks.self-529586d2fc0e99ea8f36c7780b7c89ac7ff410ba357b533fdcb64eba7b490735.js?body=1?body=1 (line 1421) 

Finished in 0.01700 seconds 
3 examples, 1 failure 

Failed examples: 

teaspoon -s default --filter="CustomerSearchController Fetching Search Results populates the customer list with the results" 
rake teaspoon failed 

私はすべての著者が行ったステップに従うことを試みたが、代わりに渡すのでは、このエラーが発生します。誰もがここで問題を見ることができますか?

答えて

0

私はそれを考え出した:私はこれを忘れてしまった:

beforeEach(function() { 
     httpBackend.when('GET','/customers.json?keywords=bob&page=0'). 
     respond(serverResults); 
    }); 

このコードを追加する場所を著者が、それは完全には明らかにしなかったので、私は間違った場所でそれを追加しました。作業コードは次のようになります。

describe("CustomerSearchController", function() { 
    describe("Initialization", function() { 
     var scope = null, 
     controller = null; 
     beforeEach(module("customers")); 
     beforeEach(inject(function ($controller, $rootScope) { 
      scope = $rootScope.$new(); 
      controller = $controller("CustomerSearchController", { 
       $scope: scope 
      }); 
     })); 

     it("defaults to an empty customer list", function() { 
      expect(scope.customers).toEqualData([]); 
     }); 

    }); 

    // Problematic code here: 
    describe("Fetching Search Results", function() { 
     beforeEach(module("customers")); 


     var scope = null, 
      controller = null, 
      httpBackend = null, 
      serverResults = [ 
       { 
        id: 123, 
        first_name: "Bob", 
        last_name: "Jones", 
        email: "[email protected]", 
        username: "jonesy" 
       }, 
       { 
        id: 456, 
        first_name: "Bob", 
        last_name: "Johnsons", 
        email: "[email protected]", 
        username: "bobbyj" 
       } 
      ]; 

     beforeEach(inject(function ($controller, $rootScope, $httpBackend) { 
      scope = $rootScope.$new(); 
      httpBackend = $httpBackend; 
      controller = $controller("CustomerSearchController", { 
       $scope: scope 
      }); 
     })); 

     beforeEach(function() { 
      httpBackend.when('GET','/customers.json?keywords=bob&page=0'). 
      respond(serverResults); 
     }); 

     it("populates the customer list with the results", function() { 
      scope.search("bob"); 
      httpBackend.flush(); 
      expect(scope.customers).toEqualData(serverResults); 
     }); 
    }); 
    }); 
関連する問題