2016-05-11 5 views
0

viewmodelからオブジェクトを受け取ったディレクティブをテストしたい。この指示文は期待通りに機能しますが、テストを書くことはできません。私はそれが渡されたオブジェクトと関係があると思う。単純な値を持つ他のディレクティブテストはうまく動作します。ObjectをパラメータとするAngularディレクティブのテスト

<dire status="ctrl.myObject"></dire> 

テスト:

var compile, scope, directiveElem; 

beforeEach(function() { 
module('App'); 
inject(function ($compile, $rootScope) { 
    compile = $compile; 
    scope = $rootScope.$new(); 
    scope.myMock = { 
    prop0: true, 
    prop1: true 
    }; 

    directiveElem = getCompiledElement(); 
}); 

}); 

function getCompiledElement() { 
    var testDirective = '<dire status="' + scope.myMock + '"></dire>'; 
    var compiledElement = compile(testDirective)(scope); 
    scope.$digest(); 
    return compiledElement; 
    } 

it('should have template', function() { 
    var spanElement = directiveElem.find('dire'); 
    expect(spanElement).toBeDefined(); 
    }); 

テストは、次のエラーで失敗します。私は、誰かが私をサポートすることを願って

Directive: dire should have template FAILED 
    Error: [$parse:syntax] Syntax Error: Token 'Object' is unexpected, expecting []] at column 9 of the expression [[object Object]] starting at [Object]]. 
    http://errors.angularjs.org/1.4.9/$parse/syntax?p0=Object&p1=is%20unexpected%2C%20expecting%20%5B%5D%5D&p2=9&p3=%5Bobject%20Object%5D&p4=Object%5D 

。ありがとう。あなたは非常に単純にできるよう

+1

これは* *ディレクティブに値を渡す方法はありません。 'var testDirective = '';'を使用してください。 'scope.myMock'は既に設定されていて、あなたはOKです。 –

+0

それはうまく動作します! ディレクティブに単純な値を渡すのが正しい方法はありますか? scope.val = 1;私は ''と合格します。 – roschulze

+0

はい、もちろん、それは同じ原則です! –

答えて

0

myMockはすでに、コンパイル時に含まれているscopeに設定されている:

scope.myMock = { 
    prop0: true, 
    prop1: true 
}; 
... 
var testDirective = '<dire status="myMock"></dire>'; 
var compiledElement = compile(testDirective)(scope); <-- scope 
関連する問題