2016-02-08 8 views
7

Vue.jsコンポーネントをテストしたいと思います。簡単に言えば、私はコンポーネントプロパティを設定しており、正しく設定されていると主張したいと思います。それが重要であれば、モジュールにエクスポートがロードされ、Webpackを使用してJSが出力されます。Vue.jsコンポーネントをテストする

// component 
exports = module.exports = {}; 

module.exports = { 
    data: function() { 
    return { 
     active: false 
    }; 
    }, 
    methods: { 
    'close': function() { 
     console.log(this.active); // -> true 
     this.active = false; 
     console.log(this.active); // -> false 
    } 
    } 
}; 

// component-test 
var modal = require('../../resources/src/js/components/_component.js'); 
var assert = require('assert'); 

describe('close()', function() { 
    beforeEach(function() { 
    modal.data.active = true; 
    }); 
    it('should set modal to inactive', function() { 
    console.log(modal.data.active); // -> true 
    modal.methods.close(); 
    console.log(modal.data.active); // -> true 
    assert.equal(modal.data.active, false); 
    }); 
}); 
+0

にアサーションを作るために、セットアップを得ることにドキュメントをチェックアウト今あるので、何が実際に失敗していますか?あなたのテストの結果は何ですか?テストについて[vue js guide](http://vuejs.org/guide/application.html#Unit_Testing)を確認してください。 –

+0

また、githubの[webpack example](https://github.com/vuejs/vue-loader-example)をチェックし、何らかの理由でカルマ+ジャスミン+ファントムジ –

答えて

6

これは、テスト時にvueコンポーネントを読み込む方法についてのヒントを提供します。

var modalComponent = require('../../resources/src/js/components/_component.js'); 
var assert = require('assert');   

//load the component with a vue instance 
vm = new Vue({ 
    template: '<div><test v-ref:test-component></test></div>', 
    components: { 
     'test': modalComponent 
    } 
}).$mount(); 

var modal = vm.$refs.testComponent; 

describe('close()', function() { 
    beforeEach(function() { 
     modal.active = true; 
    }); 

    it('should set modal to inactive', function() { 
     console.log(modal.active); // -> true 
     modal.close(); 
     console.log(modal.active); // -> false 
     assert.equal(modal.active, false); 
    }); 
}); 
+0

で定義されているテストがあります。 PhantomJSがタイムアウトになります。 PhantomJS 2.1.1(Mac OS X 0.0.0):実行された0のうち4つDISCONNECTED(10.003 secs/0 secs) ' –

関連する問題