Ionic Couchbase Lite Karma茉莉花单元测试

Sum*_*eed 10 jasmine couchbase-lite karma-jasmine ionic-framework

经过一番努力,我成功地使用业力进行茉莉花测试,但我似乎无法找到这个问题的答案:

如何在实际设备上运行我的茉莉花测试以测试与相关的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();
        });  
    });
Run Code Online (Sandbox Code Playgroud)

所以我需要在实际设备上运行测试,以便可以成功创建数据库.我是单元测试的新手,目前只使用karam 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;
     };
Run Code Online (Sandbox Code Playgroud)

Sum*_*eed 0

  1. 创建一个名为testswww/ 的文件夹

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

    A。将lib文件夹放入www/tests

    b. 复制SpecRunner.htmlwww/

  1. 让你SpecRunner.html看起来和你一模一样index.html

  1. SpecRunner.html然后在之前添加 jasmine css 和脚本</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> 
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在标签末尾body添加 jasmine lib 脚本:

    <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>
    
    Run Code Online (Sandbox Code Playgroud)

  1. 打开boot.jswww/tests/lib/jasmine-x.x.x/boot.js

    查找window.load该函数并替换为:

      window.onload = function() {
        if (currentWindowOnload) {
          currentWindowOnload();
        }
        jasmine.initialize  = htmlReporter.initialize;
        jasmine.execute     = env.execute;
      };
    
    Run Code Online (Sandbox Code Playgroud)

  1. 在起始页面的 ctrl 中,在所有内容加载后添加以下内容:

         if(window.jasmine){  
             console.log("---------------------------------------------------------");
             console.log("STARTING JASMINE...");  
             jasmine.initialize();
             jasmine.execute(); 
             console.log("---------------------------------------------------------");
             console.log("JASMINE INITIALED");
             console.log("---------------------------------------------------------");
         }
    
    Run Code Online (Sandbox Code Playgroud)

    我个人手动引导 Angular,所以我在 Angular 引导并加载主 Ctrl 后启动 jasmine:

    window.ionic.Platform.ready(function() {
        console.log("device ready");  
        angular.element(document).ready(function() {
            angular.bootstrap(document, ['myApp']); 
        }); 
    }); 
    
    Run Code Online (Sandbox Code Playgroud)

    然后,在从 Couchbase 加载文档后,我在 Ctrl 中启动 jasmine。

  1. 最后运行测试:

    重命名index.htmlindex_backup.html 重命名SpecRunner.htmlindex.html

    并运行ionic run android --device

  1. 使用 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 "test already set"; \
        fi
    
    unset-test:
        @if [ -f "www/SpecRunner.html" ]; \
        then \
            echo "test already unset"; \
        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 
    
    Run Code Online (Sandbox Code Playgroud)
  2. 样品测试

    describe('AccountsCtrl', function() { 
    
        var $scope;
        var app; 
        var $ionicSideMenu;
        var helper;
    
        var params = {
            name : 'Test Person',
            id: '112654'
        };
    
    
        beforeAll(function(done){  
            app = AppService;
            helper = getService("ActivitiesHelper");  
            $state.go('app.activities',params);  
    
        // wait for the state to change
            setTimeout(function(){ 
                $scope = getScope("activities"); 
                done();
            },1000) 
    
        });
    
    
        it('expects $scope.app to be defined', function() { 
            expect($scope.app).toBeDefined();
        });     
    
    
    });
    
    Run Code Online (Sandbox Code Playgroud)