我跟随着这本书" Rails,Angular,Postgres和Bootstrap"(第114页),我遇到了一个问题,作者并没有这样做。
第一章是关于在rails应用程序中测试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: "bjones@foo.net",
username: "jonesy"
},
{
id: 456,
first_name: "Bob",
last_name: "Johnsons",
email: "johnboy@bar.info",
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 :(得分: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: "bjones@foo.net",
username: "jonesy"
},
{
id: 456,
first_name: "Bob",
last_name: "Johnsons",
email: "johnboy@bar.info",
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);
});
});
});