Javascript 未定义:value.push(新资源(项目)
我试图测试一个工厂,但我遇到了一个奇怪的错误。我环顾四周,但没有发现类似的问题。你知道我做错了什么吗Javascript 未定义:value.push(新资源(项目),javascript,angularjs,unit-testing,karma-jasmine,Javascript,Angularjs,Unit Testing,Karma Jasmine,我试图测试一个工厂,但我遇到了一个奇怪的错误。我环顾四周,但没有发现类似的问题。你知道我做错了什么吗 TypeError:“undefined”不是函数(计算“value.push(新资源(项目))) 使用angluarjsv1.3.20 factory.js test.js describe('My Service', function () { // Then we can start by loading the main application module befo
TypeError:“undefined”不是函数(计算“value.push(新资源(项目)))
使用angluarjsv1.3.20
factory.js
test.js
describe('My Service', function () {
// Then we can start by loading the main application module
beforeEach(module(ApplicationConfiguration.applicationModuleName));
afterEach(inject(function($httpBackend){
//These two calls will make sure that at the end of the test, all expected http calls were made
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
}));
it('mock http call', inject(function($httpBackend, Business) {
var resource = new Business({
_id:'abcd'
});
var arraya = [{
_id:'abcd'
}, {
_id:'abcde'
}];
//Create an expectation for the correct url, and respond with a mock object
$httpBackend.expectGET('api/businesses/abcd').respond(200, arraya)
resource.$query();
//Because we're mocking an async action, ngMock provides a method for us to explicitly flush the request
$httpBackend.flush();
//Now the resource should behave as expected
console.log(resource);
//expect(resource.name).toBe('test');
}));
});
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX你不应该只在脸红后才期待吗
resource.$query();
//Because we're mocking an async action, ngMock provides a method for us to explicitly flush the request
$httpBackend.flush();
//Create an expectation for the correct url, and respond with a mock object
$httpBackend.expectGET('api/businesses/abcd').respond(200, arraya);
这可能很愚蠢,但你不应该把
expectGET
放在flush()
之后吗?不。当我更改顺序时,我得到错误:意外请求:获取api/business/abcd
嗯..我告诉过你,这可能很愚蠢:P对不起,我帮不上忙谢谢你的努力。我要更新angular&看看这是否奏效。
resource.$query();
//Because we're mocking an async action, ngMock provides a method for us to explicitly flush the request
$httpBackend.flush();
//Create an expectation for the correct url, and respond with a mock object
$httpBackend.expectGET('api/businesses/abcd').respond(200, arraya);