gpt4 book ai didi

node.js - Api 单元测试

转载 作者:行者123 更新时间:2023-11-28 21:31:02 25 4
gpt4 key购买 nike

我正在尝试为带来数据列表的 api 编写单元测试。如果用户已登录,则只能访问 api

由于我是单元测试的新手,所以我不知道该怎么做。以下是我到目前为止尝试的代码。

路由.js

app.get('/api/practices/page/:pagenum', middleware.auth, practices.index);

测试实践.js

describe('Practice Service API to check authentication', function() {

var httpBackend,
serviceUrl = '/api/practices/page/?pagenum=1';

beforeEach(function () {
module('myApp');

inject(function ($httpBackend) {
httpBackend = $httpBackend;
});
});

// check to see if User is Authenticated
it('should have an authenticated user', function() {
// To get response if user is not authenticated
//serviceUrl.expectGET(serviceUrl).respond();

});

});

具体怎么做?

最佳答案

根据您提供的小信息,这是一些非常人为的伪代码。我希望它至少能让你入门!

describe('Practice Service API to check authentication', function() {

var httpBackend,
yourService,
yourResponse;

beforeEach(function () {
module('myApp');
});

beforeEach(inject(function ($httpBackend, ServiceNameHere) {
yourService = ServiceNameHere;
httpBackend = $httpBackend;
httpBackend.when('GET', '/api/practices/page/:pagenum').respond(yourResponse);
}));


describe('Your Service Functionality', function () {
// check to see if User is Authenticated
it('should have an authenticated user', function() {
yourService.methodToCheckLogin();
expect('someexpectation of login events').toBeTruthy();
});
});

});

关于node.js - Api 单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22404413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com