2017-01-12 10 views
0

私はテスト目的のために私のアプリケーションでいくつかのHTTPトラフィックを傍受/模擬するためにnockを使用しようとしています。私たちのアプリは、私たちのサイトの別のサイトに認証して、HTTP 200(JSONデータ)とHTTP 401(データなし)を模倣してノックする必要があります。 。NockとMochaを一緒にうまく動かすにはどうすればいいですか?

私は2つのテストを単独で実行すると正しく動作しますが、テストスイート全体を実行するとそのうちの1つは常に失敗します。ノックは、node.js自体がネットワークトラフィックをどのように処理するのかを変更し、それが競合状態の原因であると仮定しているため、ノックが共有状態になっていることに気付きました。しかし、同じリクエストに対して2つの異なるノックインターセプタを使用した唯一の人2つの異なるテスト、私は何かが不足していることを知っている。

誰も私がなぜこれらのテストが互いに踏み込んでいるのか理解できませんか?

私の質問はHow to retest same URL using Mocha and Nock?に関連していますが、私はそこに示唆されたことをしたし、彼らは助けなかった。

(、再び、両者が個別に呼び出されますが、同じテストパスの一部として実行したときに失敗した場合は正常に動作)私のテストファイルは次のようになります。

import { expect } from 'chai'; 
import nock from 'nock'; 

import * as actionTypes from '../../src/constants/action-types'; 
import * as panoptes from '../../src/services/panoptes'; 

import { user } from '../modules/users/test-data'; 

const stagingHost = 'https://my-staging-server.org'; 

describe('Panoptes',() => { 
    afterEach(function (done) { 
    nock.cleanAll(); 
    nock.disableNetConnect(); 
    done(); 
    }); 

    beforeEach(function (done) { 
    nock.cleanAll(); 
    nock.disableNetConnect(); 
    done(); 
    }); 

    describe('with a valid user', function (done) { 
    let lastAction = null; 

    const scope = nock(stagingHost) 
     .get(/^\/oauth\/authorize/) 
     .reply(302, '', { 
     'location': 'https://localhost:3000', 
     'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', 
     'X-Frame-Options': 'SAMEORIGIN', 
     'X-XSS-Protection': '1; mode=block', 
     }); 

    scope 
     .get(/^\/api\/me/) 
     .reply(200, { 
     users: [user], 
     }); 

    panoptes.checkLoginUser((action) => { lastAction = action; }).then(() => { 
     nock.removeInterceptor(scope); 
     done(); 
    }); 

    it('should know when somebody is logged in', function() { 
     expect(lastAction).to.not.be.null; 
     expect(lastAction.type).to.equal(actionTypes.SET_LOGIN_USER); 
     expect(lastAction.user).to.not.be.null; 
     expect(lastAction.user.id).to.equal(user.id); 
     expect(lastAction.user.login).to.equal(user.login); 
    }); 
    }); 
}); 

import { expect } from 'chai'; 
import nock from 'nock'; 

import * as actionTypes from '../../src/constants/action-types'; 
import * as panoptes from '../../src/services/panoptes'; 

const stagingHost = 'https://my-staging-server.org'; 

describe('Panoptes',() => { 
    afterEach(function (done) { 
    nock.cleanAll(); 
    nock.disableNetConnect(); 
    done(); 
    }); 

    beforeEach(function (done) { 
    nock.cleanAll(); 
    nock.disableNetConnect(); 
    done(); 
    }); 

    describe('with no user', function (done) { 
    let lastAction = null; 

    const scope = nock(stagingHost) 
     .get(/^\/oauth\/authorize/) 
     .reply(302, '', { 
     'Cache-Control': 'no-cache', 
     'location': 'https://my-staging-server.org/users/sign_in', 
     'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', 
     'X-Frame-Options': 'SAMEORIGIN', 
     'X-XSS-Protection': '1; mode=block', 
     }); 

    scope 
     .get(/^\/api\/me/) 
     .reply(401); 

    panoptes.checkLoginUser((action) => { lastAction = action; }).then(() => { 
     nock.removeInterceptor(scope); 
     done(); 
    }); 

    it('should know that nobody is logged in', function() { 
     expect(lastAction).to.not.be.null; 
     expect(lastAction.type).to.equal(actionTypes.SET_LOGIN_USER); 
     expect(lastAction.user).to.be.null; 
    }); 
    }); 
}); 
+0

両方のテストを一緒に実行すると、どのようなエラーが表示されますか? –

+0

私はエラーは発生しませんが、実際には2番目のテストが失敗したときに失敗しただけです。 2回目のテストは、最初のテストの模擬試験の影響を受けているようです。 –

答えて

0

問題はノックではなく、モカフックの実行命令の順番であると思います。

describe('Panoptes',() => { 

    afterEach(function() { 
    console.log('ORDER: after each'); 
    }); 

    beforeEach(function() { 
    console.log('ORDER: before each'); 
    }); 

    describe('with a valid user', function() { 

    console.log('ORDER: with a valid user'); 

    it('should know when somebody is logged in', function() { 
     console.log('ORDER: should know when somebody is logged in'); 
    }); 

    }); 

    describe('with no user', function() { 

    console.log('ORDER: with no user'); 

    it('should know that nobody is logged in', function() { 
     console.log('ORDER: should know that nobody is logged in'); 
    }); 

    }); 

}); 

我々は出力に次の順序を取得し、それを実行すると:

ORDER: with a valid user 
ORDER: with no user 
ORDER: before each 
ORDER: should know when somebody is logged in 
ORDER: after each 
ORDER: before each 
ORDER: should know that nobody is logged in 
ORDER: after each 

afterEach/beforeEach実行する前とitそれぞれの後に、これらのフックが呼び出される前に、しかしdescribe体が評価されます。 beforeの中にあなたの両足を包むべきです。このような

何かが動作するはずです(またdescribedone引数を使用しません):

describe('with no user', function() { 

    before(function() { 
    const scope = nock(stagingHost) 
     .get(/^\/oauth\/authorize/) 
     .reply(302, '', { 
     'Cache-Control': 'no-cache', 
     'location': 'https://my-staging-server.org/users/sign_in', 
     'Strict-Transport-Security': 'max-age=31536000; includeSubDomains', 
     'X-Frame-Options': 'SAMEORIGIN', 
     'X-XSS-Protection': '1; mode=block', 
     }); 

    scope 
     .get(/^\/api\/me/) 
     .reply(401); 
    }); 


    it('should know that nobody is logged in', function (done) { 
    panoptes.checkLoginUser((action) => { 
     expect(action).to.not.be.null; 
     expect(action.type).to.equal(actionTypes.SET_LOGIN_USER); 
     expect(action.user).to.be.null; 
     done(); 
    }); 
    }); 

}); 
+0

ありがとうございました。私はそれを試してみて、それが問題を解決するかどうかを見たらコメントします。 –

+0

これはまさに私が必要としたことでした。残念ながら、私のテストはまだ失敗していますが、今はエラーが私たちのコードにあります。私が前に間違ってテストしていたので、私はそれを見ていませんでした! –

+0

うれしかった!がんばろう! –

0

私は解決のための信用を与えているsanketh・刈田の答えを、使用したが、わずかitブロックを修正ので、私は完全性についての私のコードを含めています:

it('should know when somebody is logged in', function(done) { 
    panoptes.checkLoginUser((action) => { 
    try { 
     expect(action).to.not.be.null; 
     expect(action.type).to.equal(actionTypes.SET_LOGIN_USER); 
     expect(action.user).to.not.be.null; 
     expect(action.user.id).to.equal(user.id); 
     expect(action.user.login).to.equal(user.login); 
     done(); 
    } catch (ex) { 
     done(ex); 
    } 
    }); 
}); 

試験の一つが失敗したときに、done()呼び出しが到達することはないだろう、と私はテストがタイムアウトしたというメッセージを得るだろう、前にイン特定の失敗の話。

+0

あなたは[chai-as-promised](https:// github。com/domenic/chai-as-promised)を使用して、正確な問題を解決します。 –

関連する問題