api的单元测试

安纳普

我正在尝试编写api带数据列表的单元测试所述api罐磺酰基是访问如果用户登录。

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

routes.js

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

testpractice.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();

    });

});

究竟如何做到这一点?

斯坦·莫奇(Sten Muchow)

这是根据您提供的少量信息而设计的一些非常虚假的伪代码。我希望它至少可以帮助您入门!

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();
        });
    });

});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章