From 3b13bc0a9d8ca6a4d6af6b730b12f17f7ee20427 Mon Sep 17 00:00:00 2001 From: Mikhail Losev Date: Tue, 24 Apr 2018 16:05:10 +0300 Subject: [PATCH] Request mock should handle only specified requests (part of #2336) (#2339) --- src/api/request-hooks/request-mock.js | 2 +- test/server/request-hooks-test.js | 77 +++++++++++++++------------ 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/src/api/request-hooks/request-mock.js b/src/api/request-hooks/request-mock.js index 050af496ffc..bdc14de7b44 100644 --- a/src/api/request-hooks/request-mock.js +++ b/src/api/request-hooks/request-mock.js @@ -4,7 +4,7 @@ import { RequestHookConfigureAPIError } from '../../errors/test-run/index'; class RequestMock extends RequestHook { constructor () { - super(); + super([]); this.pendingRequestFilterRuleInit = null; this.mocks = new Map(); diff --git a/test/server/request-hooks-test.js b/test/server/request-hooks-test.js index df8139fdbcb..a57fa1ad979 100644 --- a/test/server/request-hooks-test.js +++ b/test/server/request-hooks-test.js @@ -183,46 +183,57 @@ describe('RequestLogger', () => { }); }); -describe('RequestMock (throwing errors)', () => { - describe('Chaining', () => { - it('.respond().onRequestTo()', () => { - assertThrow(() => { - RequestMock().respond(noop).onRequestTo({}); - }, { - isTestCafeError: true, - requestHookName: 'RequestMock', - errMsg: "The 'onRequestTo' method was not called before 'respond'. You must call the 'onRequestTo' method to provide the URL requests to which are mocked.", - type: 'requestHookConfigureAPIError', - callsite: null +describe('RequestMock', () => { + describe('Throwing errors', () => { + describe('Chaining', () => { + it('.respond().onRequestTo()', () => { + assertThrow(() => { + RequestMock().respond(noop).onRequestTo({}); + }, { + isTestCafeError: true, + requestHookName: 'RequestMock', + errMsg: "The 'onRequestTo' method was not called before 'respond'. You must call the 'onRequestTo' method to provide the URL requests to which are mocked.", + type: 'requestHookConfigureAPIError', + callsite: null + }); + }); + + it('onRequestTo().onRequestTo()', () => { + assertThrow(() => { + RequestMock().onRequestTo({}).onRequestTo({}); + }, { + isTestCafeError: true, + requestHookName: 'RequestMock', + errMsg: "The 'respond' method was not called after 'onRequestTo'. You must call the 'respond' method to provide the mocked response.", + type: 'requestHookConfigureAPIError', + callsite: null + }); }); }); - it('onRequestTo().onRequestTo()', () => { - assertThrow(() => { - RequestMock().onRequestTo({}).onRequestTo({}); - }, { - isTestCafeError: true, - requestHookName: 'RequestMock', - errMsg: "The 'respond' method was not called after 'onRequestTo'. You must call the 'respond' method to provide the mocked response.", - type: 'requestHookConfigureAPIError', - callsite: null + describe('Construction', () => { + it('Without configure', () => { + expect(() => { + RequestMock(); + }).to.not.throw; + }); + it('With configure', () => { + expect(() => { + RequestMock() + .onRequestTo('http://example.com') + .respond(''); + }).to.not.throw; }); }); }); - describe('Construction', () => { - it('Without configure', () => { - expect(() => { - RequestMock(); - }).to.not.throw; - }); - it('With configure', () => { - expect(() => { - RequestMock() - .onRequestTo('http://example.com') - .respond(''); - }).to.not.throw; - }); + it('Should handle only specified requests (GH-2336)', () => { + const mock = RequestMock() + .onRequestTo('http://example.com') + .respond(); + + expect(mock.requestFilterRules.length).eql(1); + expect(mock.requestFilterRules[0].options.url).eql('http://example.com'); }); });