【问题标题】:Unit Test for ApiApi 的单元测试
【发布时间】:2014-03-14 12:00:50
【问题描述】:

我正在尝试为带来数据列表的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();

    });

});

具体如何做到这一点?

【问题讨论】:

    标签: node.js angularjs unit-testing testing


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 2015-07-30
      • 2012-03-18
      • 2014-07-15
      • 2011-06-01
      • 1970-01-01
      • 2019-04-18
      相关资源
      最近更新 更多