-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use Test::LWP::Recorder to mock requests #26
base: master
Are you sure you want to change the base?
Conversation
Omg yay |
This is fantastic. I'm reviewing, should be OK but I just want to familiarize myself with the changes. |
@@ -62,12 +65,20 @@ sub skip_unless_has_secret { | |||
plan skip_all => 'PERL_STRIPE_TEST_API_KEY is required' unless api_key(); | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need skip_unless_has_secret
anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to keep it because t/07-options.t
still needs it. We can't use a mock in the idempotency_key
test https://github.com/Crowdtilt/WebService-Stripe/blob/lwp-recorder/t/07-options.t#L17
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would filter_header => 'Idempotency-Key'
solve that? I'm not sure if that affects how LWP Recorder would match a request with a response.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it wouldn't solve it. The problem is that LWP Recorder has no way to differentiate between 2 identical requests. It would only store the response for one of them. That would make mocking this test useless.
This mocks our tests so that a stripe key or network connection will not be necessary to run the tests. This also gives a major speedup. We can still run real tests hitting the actual stripe webservice as we do now, if a stripe api key is provided.
The tests run in under 1s now:
|
state $client = WebService::Stripe->new( | ||
api_key => api_key(), | ||
version => '2014-12-17', | ||
%params | ||
%params, | ||
); | ||
return $client; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't like no_
-prefixed variables. Affirmative variables (e.g. mock_lwp => 1
) tend to be clearer / less confusing. Suggested rewrite:
memoize 'stripe';
sub stripe {
my %params = @_;
$params{'api_key'} //= api_key;
$params{'version'} //= '2014-12-17';
$params{'mock_lwp_record'} //= $ENV{'PERL_STRIPE_MOCK_RECORD'};
$params{'mock_lwp'} //= $ENV{'PERL_STRIPE_MOCK'}
// !$params{'api_key'};
my %client;
$client{'api_key'} = $params{'api_key'};
$client{'version'} = $params{'version'};
$client{'ua'} = Test::LWP::Recorder->new({
record => $params{'mock_lwp_record'},
cache_dir => 't/LWPCache/' . basename($0),
}) if $params{'mock_lwp'};
state $stripe = WebService::Stripe->new(%client);
return $stripe;
}
👍 Had a Q about using |
We can't merge this until this patch gets accepted riemann42/Test-LWP-Recorder#3 , or the author lets me take over that module, or we aggressively fork it. |
Another option is to use this module instead https://metacpan.org/pod/Test::VCR::LWP |
That module seems much harder to use suite-wide than the current one. |
This mocks our tests so that a stripe key or network connection will not
be necessary to run the tests. This also gives a major speedup. We can
still run real tests hitting the actual stripe webservice as we do now, if
a stripe api key is provided.