我正在测试依赖于API的AngularJS应用程序,我是新的API测试。我登录到AngularJS app的页面。当我登录它时调用api我能够测试UI的东西,但现在我需要测试API。
例如UI代码,我正在使用页面对象:
LoginPage.setUserName('test@test.com');
LoginPage.setPassWord('test');
LoginPage.clickButton();
此代码工作正常,点击提交按钮,它调用API来处理登录。现在我需要测试那个API调用:
点击它调用:
GET https://192.168.00.00:0000/config/settings.json
回复:
{
"domain": "https://192.168.000.00",
"port":1111
}
然后调用:
POST https://192.168.000.00:1111/api/Login
回复:
{
"status":"SUCCESS",
"message":"Login Successful",
"results: {
"id":96,
"userName":"test@test.com",
"password":null,
"firstName":"Test",
"lastName":"Testing",
"employeeCode":"007",
"location":2
}
}
现在我需要通过嘲笑来测试这个场景。我写了这个嘲弄的代码:
exports.signup_request = function() {
angular.module('myApp', ['ngMockE2E']).run(function($httpBackend) {
$httpBackend.whenGET('/api/Login/').respond({
"status":"SUCCESS",
"message":"Login Successful",
"results": {
"id":96,
"userName":"test@test.com",
"password":null,
"firstName":"test",
"lastName":"Testing",
"employeeCode":"007",
"location":2
}
});
// allow views directory to go to real $http
$httpBackend.whenGET(/^\/views\//).passThrough();
// we have a real /api/customer service, let it through
$httpBackend.whenGET(/^/api/Login//).passThrough();
});
}
我的spec文件加载了我编写$httpBackend
相关代码的模拟js文件:
beforeEach(function(){
mockModule = require('./mock');
browser.ignoreSynchronization = true;
});
it("ensure user can signup for the application", function(){
browser.addMockModule('httpBackendMock', mockModule.signup_request);
// DONT NO WHAT TO WRITE HERE;
});
当我运行此规范时,它显示browser.addMockModule('httpBackendMock', mockModule.signup_request);
未定义。