1

jasmine-node テストを使用して、いくつかの外部 API テストをテストしようとしています。ただし、テスト スイート全体を実行しても、基本的な接続が機能する場合にのみ意味があります。つまり、これは基本的に、単純な ping テストから他のすべての人にその情報を渡す必要があることを意味します。

それが私が試したことですが、最初のテストに合格してもこれは成功しません:

var canConnect = false;
describe("The API client", function () {
    it("can connect server", function (done) {
        api.ping(function (err) {
            expect(err).toBeNull();
            canConnect = true;
            done();
        })
    });

    // pointless the run these if the ping didn't work
    if (canConnect) describe("connected clients", function () {
        it("can use the API", function (done) {
            api.someOtherRequest(function(err) {
                expect(err).toBeUndefined();
                done();
            });
        })

    });
})

助言がありますか?たぶん、これをよりスマートに解決する方法はありますか?

乾杯

4

1 に答える 1

1

クライアントとして呼び出して Api 関数をテストしようとしているのはなぜですか? あなたが API の開発者である場合、API サーバー側の機能をテストしてから、クライアント テスト用にモックする必要があるようです。

beforeEachただし、これを行う必要がある場合は、各テストの前に 内のすべてが実行されることを保証できます。ping テストをアウターに配置describeして最初に実行し、次にbeforeEachネストされた のdescribeで接続を確認します。

describe('first block',function(){

  beforeEach(function(){
    //do initialization
  });

  it('should do the pinging',function(){
    //normally test your Api ping test here
    expect('this test called first').toBe('this test called first');
  });

  describe('now that we have tested pinging',function(){
    var canConnect;

    var checkConnectible = function(){

            ////Normally do your api ping check here, but to demonstrate my point
            ////I've faked the return 
            //api.ping(function (err) {
            //if (typeof(err) !== "undefined" && err !== null){
            //  return true;
            //}
            //return false

            //to see the main point of this example
            //change this faked return to false or true:
            return false;
    };

    beforeEach(function(){
      canConnect = checkConnectible();
    });

    it('should only be run if canConnect is true',function(){
      if (canConnect){
        //expect
      }
      else{
        expect('').toBe('cannot run this test until connectible');
      }
    });
  });
});

各テストの前に接続チェックが行われるため、チェックの結果に基づいて異なる結果を出すことができます。また、checkConnectible で何らかのタイムアウト チェックを行い、それに応じて true または false を返すこともできます。

于 2014-02-27T16:50:48.010 に答える