Ionic Couchbase Lite Karma Jasmine 单元测试

10

经过一番努力,我成功地使用karma运行了茉莉测试,但是我似乎找不到这个问题的答案:

我该如何在实际设备上运行我的茉莉测试以测试与couchbase lite数据库相关的功能?

我正在使用这个:https://github.com/couchbaselabs/ng-couchbase-lite

这是我的测试:

     describe('SetupService tests', function() {
        
        it('SetupService should instantiate', function() { 
            expect(SetupService).toBeDefined();
        }); 
        
        it('it should instantiate database', function() { 
            var database = null; 
            SetupService.setupConfig();
            expect(database).not.toBeNull();
        });  
    });

所以我需要在实际设备上运行测试,这样数据库才能成功创建。我对单元测试还很陌生,目前仅使用karma cli。

设置配置显示需要couchbase lite和cordova:

    var setupConfig = function() {
         console.log("set up config");
         var deferred = $q.defer();

         if(!window.cblite) { 
             deferred.reject('Couchbase Lite not installed');  
         } 
         else {
             cblite.getURL(function(err, url) {
                 console.log("cblite get url");
                 if(err) {
                     console.log(err);
                     deferred.reject("There was an error getting the database URL");  
                 }
                 else{
                     database = new $couchbase(url, appDbName);  
                     database.createDatabase().then(function(result) {
                         var views = setupViews();
                         database.createDesignDocument("_design/todo", views);
                         database.listen();
                         deferred.resolve(true); 
                     }, function(error) {
                         // we will not reject this err, as it is caused when a db already exists
                         // so it will happen everytime
                         deferred.resolve(err);  
                     });
                 } 
             }); 
         } 

         return deferred.promise;
     };

你能不能不使用Appium或Selenium在设备上启动应用程序,然后在其运行时执行这些测试呢? - Stacker-flow
1个回答

0
  1. 在www/中创建一个名为tests的文件夹。

    .

  2. 这里下载最新的独立jasmine zip文件

    a. 将lib文件夹放入www/tests

    b. 将SpecRunner.html复制到www/

.

  1. 让你的 SpecRunner.html 看起来和你的 index.html 完全一样

.

  • 然后将jasmine css和scripts添加到SpecRunner.html中,在</head>之前:

    <link rel="shortcut icon" type="image/png" href="tests/lib/jasmine-x.x.x/jasmine_favicon.png">
    <link rel="stylesheet" href="tests/lib/jasmine-x.x.x/jasmine.css">
    <style>
        .jasmine_html-reporter{
            width: 100%;
            margin: 200px 0px;
        } 
    </style> 
    
  • body标签的末尾添加jasmine lib scripts:

    <script src="tests/lib/jasmine-x.x.x/jasmine.js"></script>
    <script src="tests/lib/jasmine-x.x.x/jasmine-html.js"></script>
    <script src="tests/lib/jasmine-x.x.x/boot.js"></script>
    
  • .

  • 打开 www/tests/lib/jasmine-x.x.x/boot.js 中的 boot.js

    查找 window.load 函数并替换为以下内容:

      window.onload = function() {
        if (currentWindowOnload) {
          currentWindowOnload();
        }
        jasmine.initialize  = htmlReporter.initialize;
        jasmine.execute     = env.execute;
      };
    
  • .

    在您的起始页面控制函数中,当所有内容加载完成时添加以下代码:
    ```javascript if (window.jasmine) { console.log("---------------------------------------------------------"); console.log("STARTING JASMINE..."); jasmine.initialize(); jasmine.execute(); console.log("---------------------------------------------------------"); console.log("JASMINE INITIALED"); console.log("---------------------------------------------------------"); } ```
    我个人手动引导 Angular,因此在 Angular 引导完成并且我的主要控制器已加载后才启动 Jasmine:
    ```javascript window.ionic.Platform.ready(function() { console.log("device ready"); angular.element(document).ready(function() { angular.bootstrap(document, ['myApp']); }); }); ```
    然后在我的控制器中,在从 Couchbase 加载文档后,启动 Jasmine。

    .

  • 最后运行测试:

    index.html重命名为index_backup.html, 将SpecRunner.html重命名为index.html

    然后运行ionic run android --device

  • .

  • 使用Makefile自动化步骤8:

    set-test: 
        @if [ -f "www/SpecRunner.html" ]; \
        then \
            mv www/index.html www/index_backup.html; \
            mv www/SpecRunner.html www/index.html; \
        else \
            echo "测试已设置"; \
        fi
    
    unset-test:
        @if [ -f "www/SpecRunner.html" ]; \
        then \
            echo "测试已取消"; \
        else \
            mv www/index.html www/SpecRunner.html;  \
            mv www/index_backup.html www/index.html; \
        fi       
    test:  
        make set-test    
        ionic run android --device 
    
  • 示例测试

    describe('AccountsCtrl', function() { 
    
        var $scope;
        var app; 
        var $ionicSideMenu;
        var helper;
    
        var params = {
            name : '测试人员',
            id: '112654'
        };
    
    
        beforeAll(function(done){  
            app = AppService;
            helper = getService("ActivitiesHelper");  
            $state.go('app.activities',params);  
    
        // 等待状态改变
            setTimeout(function(){ 
                $scope = getScope("activities"); 
                done();
            },1000) 
    
        });
    
    
        it('期望$scope.app被定义', function() { 
            expect($scope.app).toBeDefined();
        });     
    
    
    });
    

  • 网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接