0

Angular testing docs は、アサーションをテストする前に $apply が呼び出されるテスト サンプルを示しています。同じことをしようとしましたが、テストが正しく機能していません。壊れるはずの最初のコードが機能します...私のアサーションが実行されていないようです。2 番目のコード、私のテストは機能しますが、ドキュメントとは異なります (スタイルの方が好きです)。

初め:

it('tests angular promises', function() {
    getABC = function() {
        return $q((resolve, reject) => {
            resolve('abc');
        });
    };

    var state = getABC()

    $rootScope.$apply()
    // assertions come after $apply call as in documentation: doesn't work.
    state.should.eventually.be.equal('abcc') 
})

------
✓ tests angular promises


1 passing (78ms)

2番

it('tests angular promises', function() {
    getABC = function() {
        return $q((resolve, reject) => {
            resolve('abc');
        });
    };

    var state = getABC()

    state.should.eventually.be.equal('abcc')
    // assertions come before  $apply call: works
    $rootScope.$apply()
})

------
1 failing

1) tests angular promises:

  AssertionError: expected 'abc' to equal 'abcc'
  + expected - actual

  -abc
  +abcc
4

1 に答える 1

1

示されているは別のケースです。

それは使用しています

expect(resolvedValue).toEqual(123);

promise を含まないアサーション。

一方で、

state.should.eventually.be.equal('abcc')

chai-as-promisedアサーションに使用します。内部でチェーンstatethenて値を取得し、アサートします。また、$qpromise チェーンはダイジェストでのみ実行されます。そのため、アサーションの$rootScope.$digest()後に必要です。eventually

で Angular をテストする方法の詳細については、この回答も参照してくださいchai-as-promised

于 2017-01-03T13:54:40.860 に答える