1

成功した場合にステータス コードとパスを含むコールバックを返す非同期関数があります。jasmine を使用して、そのステータス コードを受け取り、今後のすべてのテストにパスを割り当てることをテストしたいと思います。テストで未定義の値を取得し続けます...

function make_valid(cb){
    var vdt = child.spawn('node', base,
        {cwd: ims_working_dir, stdio: ['pipe', 'pipe', 'pipe']},
        function (error, stdout, stderr) {
            if (error !== null) {
                console.log('spawn error: ' + error);
            }
        });
    vdt.stdout.setEncoding('utf8');
    vdt.stdout.on('data', function(data) {});

    vdt.on('close', function(code) {

        if (code === 0) {
            cb(null, '/home/');
        } else {
            cb(null, code);
        }
    });
}

describe("IMS", function(cb) {
    var path;
    jasmine.getEnv().defaultTimeoutInterval = 40000;

    beforeEach(function(done, cb) {
        console.log('Before Each');
        path = make_valid(cb);
        done();
    });

    it("path should not be null", function() {
        expect(path).toBeDefined();
    });
});

出力は次のとおりです。

Failures:

  1) IMS path should not be null
   Message:
     Expected undefined to be defined.
   Stacktrace:
     Error: Expected undefined to be defined.
    at null.<anonymous> (/home/evang/Dev/dataanalytics/IMS_Tester/spec/ims-spec.js:46:16)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Finished in 0.014 seconds

1 test, 1 assertion, 1 failure, 0 skipped

beforeEach 宣言でパスが渡された場合、ファイルを解析するテストをさらに記述したいと思います。コールバックを正しく処理していないか、スパイを使用する必要があると思います。お知らせ下さい!

4

1 に答える 1

0

path関数の戻り値で設定していますがmake_valid、その関数は何も返しません。

を使用したとき、テスト自体 (関数)jasmineのコールバックのみで成功しました。done()it()

各テストの前に非同期関数が必要な場合は、関数runs()waitsFor()関数を使用しました。

次のようなコードを試してください(例に合わせて変更してください):

var code = null;
beforeEach( function() {
  // get a token so we can do test cases. async so use jasmine's async support
  runs( function() {
    make_valid( function( error, returnCode ) {
      if( error ) code = "ERROR_CODE";
      else code = returnCode;
    } );
  } );

  waitsFor( function() {
    return code !== null;
  } );

  runs( function() {
    expect( code ).toBeDefined();
  } );
} );

afterEach( function() {
  code = null;
} );

EDIT : 私たちのコメントに基づいて、今後の他のテストに適用される 1 回限りの非同期テストの推奨されるアプローチは thissample-spec.jsです。外側が を使用しない場合、内側のテストはパスit()しませんdone()

/*jslint node: true */
"use strict";

describe( "The full, outer test", function() {

  var code = null;
  it( "should setup for each inner test", function(done) {
    make_valid( function( error, returnCode ) {
      expect( returnCode ).toBeDefined();
      code = returnCode;
      done();
    } );
  } );

  describe( "The inner tests", function() {
    it( "inner test1", function() {
      expect( code ).not.toBeNull();
    } );
    it( "inner test2", function() {
      expect( code ).toBe(200);
    } );
  } );
} );

function make_valid( callback ) {
  setTimeout( function(){ callback( null, 200 ); }, 500 );
}
于 2014-02-18T17:58:33.930 に答える