Skip to content

Commit

Permalink
Use StripeClient instance in tests (#1634)
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Jun 6, 2019
1 parent 21c76f3 commit 3762313
Show file tree
Hide file tree
Showing 74 changed files with 95 additions and 80 deletions.
10 changes: 6 additions & 4 deletions src/StripeTests/BaseStripeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public BaseStripeTest(
// Set up StripeClient to use stripe-mock with the mock HTTP client
var httpClient = new SystemNetHttpClient(
new HttpClient(this.MockHttpClientFixture.MockHandler.Object));
StripeConfiguration.StripeClient = this.StripeMockFixture.BuildStripeClient(
this.StripeClient = this.StripeMockFixture.BuildStripeClient(
httpClient: httpClient);

// Reset the mock before each test
Expand All @@ -68,14 +68,14 @@ public BaseStripeTest(
else if (this.StripeMockFixture != null)
{
// Set up StripeClient to use stripe-mock
StripeConfiguration.StripeClient = this.StripeMockFixture.BuildStripeClient();
this.StripeClient = this.StripeMockFixture.BuildStripeClient();
}
else if (this.MockHttpClientFixture != null)
{
// Set up StripeClient with the mock HTTP client
var httpClient = new SystemNetHttpClient(
new HttpClient(this.MockHttpClientFixture.MockHandler.Object));
StripeConfiguration.StripeClient = new StripeClient(
this.StripeClient = new StripeClient(
"sk_test_123",
httpClient: httpClient);

Expand All @@ -85,10 +85,12 @@ public BaseStripeTest(
else
{
// Use the default StripeClient
StripeConfiguration.StripeClient = null;
this.StripeClient = new StripeClient("sk_test_123");
}
}

protected IStripeClient StripeClient { get; }

protected MockHttpClientFixture MockHttpClientFixture { get; }

protected StripeMockFixture StripeMockFixture { get; }
Expand Down
2 changes: 2 additions & 0 deletions src/StripeTests/Functional/NetworkRetriesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public NetworkRetriesTest(MockHttpClientFixture mockHttpClientFixture)
{
StripeConfiguration.MaxNetworkRetries = 2;
StripeConfiguration.NetworkRetriesSleep = false;
StripeConfiguration.StripeClient = this.StripeClient;
}

public void Dispose()
{
StripeConfiguration.MaxNetworkRetries = 0;
StripeConfiguration.NetworkRetriesSleep = true;
StripeConfiguration.StripeClient = null;
this.MockHttpClientFixture.Reset();
}

Expand Down
8 changes: 4 additions & 4 deletions src/StripeTests/Infrastructure/Public/StripeResponseTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public StripeResponseTest(StripeMockFixture stripeMockFixture)
[Fact]
public void Date()
{
var response = new AccountService().GetSelf().StripeResponse;
var response = new AccountService { Client = this.StripeClient }.GetSelf().StripeResponse;

Assert.NotNull(response.Date);
}
Expand All @@ -31,23 +31,23 @@ public void Date()
public void IdempotencyKey_Present()
{
var requestOptions = new RequestOptions { IdempotencyKey = "idempotency_key" };
var response = new AccountService().GetSelf(requestOptions).StripeResponse;
var response = new AccountService { Client = this.StripeClient }.GetSelf(requestOptions).StripeResponse;

Assert.Equal("idempotency_key", response.IdempotencyKey);
}

[Fact]
public void IdempotencyKey_Absent()
{
var response = new AccountService().GetSelf().StripeResponse;
var response = new AccountService { Client = this.StripeClient }.GetSelf().StripeResponse;

Assert.Null(response.IdempotencyKey);
}

[Fact]
public void RequestId()
{
var response = new AccountService().GetSelf().StripeResponse;
var response = new AccountService { Client = this.StripeClient }.GetSelf().StripeResponse;

Assert.NotNull(response.RequestId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace StripeTests
{
using System;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand All @@ -12,11 +13,18 @@ namespace StripeTests
using Stripe;
using Xunit;

public class SystemNetHttpClientTest : BaseStripeTest
public class SystemNetHttpClientTest : BaseStripeTest, IDisposable
{
public SystemNetHttpClientTest(MockHttpClientFixture mockHttpClientFixture)
: base(mockHttpClientFixture)
{
// TODO: don't use StripeConfiguration
StripeConfiguration.StripeClient = this.StripeClient;
}

public void Dispose()
{
StripeConfiguration.StripeClient = null;
}

[Fact]
Expand Down
5 changes: 4 additions & 1 deletion src/StripeTests/Infrastructure/StripeExceptionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ public StripeExceptionTest(StripeMockFixture stripeMockFixture)
public async Task SetsStripeResponse()
{
var exception = await Assert.ThrowsAsync<StripeException>(async () =>
await new CouponService().CreateAsync(new CouponCreateOptions()));
{
await new CouponService { Client = this.StripeClient }
.CreateAsync(new CouponCreateOptions());
});

Assert.NotNull(exception);
Assert.NotNull(exception.StripeError);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public AccountLinkServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new AccountLinkService();
this.service = new AccountLinkService { Client = this.StripeClient };

this.createOptions = new AccountLinkCreateOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Accounts/AccountServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public AccountServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new AccountService();
this.service = new AccountService { Client = this.StripeClient };

this.createOptions = new AccountCreateOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public ApplePayDomainServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new ApplePayDomainService();
this.service = new ApplePayDomainService { Client = this.StripeClient };

this.createOptions = new ApplePayDomainCreateOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ApplicationFeeRefundServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new ApplicationFeeRefundService();
this.service = new ApplicationFeeRefundService { Client = this.StripeClient };

this.createOptions = new ApplicationFeeRefundCreateOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ApplicationFeeServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new ApplicationFeeService();
this.service = new ApplicationFeeService { Client = this.StripeClient };

this.listOptions = new ApplicationFeeListOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/AutoPagingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void ListAutoPaging()
.Throws(new StripeTestException("Unexpected invocation!"));

// Call auto-paging method
var service = new PageableService();
var service = new PageableService { Client = this.StripeClient };
var options = new ListOptions
{
Limit = 2,
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Balance/BalanceServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public BalanceServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new BalanceService();
this.service = new BalanceService { Client = this.StripeClient };
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public BalanceTransactionServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new BalanceTransactionService();
this.service = new BalanceTransactionService { Client = this.StripeClient };

this.listOptions = new BalanceTransactionListOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public BankAccountServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new BankAccountService();
this.service = new BankAccountService { Client = this.StripeClient };

this.createOptions = new BankAccountCreateOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public CapabilityServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new CapabilityService();
this.service = new CapabilityService { Client = this.StripeClient };

this.updateOptions = new CapabilityUpdateOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Cards/CardServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public CardServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new CardService();
this.service = new CardService { Client = this.StripeClient };

this.createOptions = new CardCreateOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Charges/ChargeServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ChargeServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new ChargeService();
this.service = new ChargeService { Client = this.StripeClient };

this.captureOptions = new ChargeCaptureOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Checkout/SessionServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public SessionServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new SessionService();
this.service = new SessionService { Client = this.StripeClient };

this.createOptions = new SessionCreateOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public CountrySpecServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new CountrySpecService();
this.service = new CountrySpecService { Client = this.StripeClient };

this.listOptions = new CountrySpecListOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Coupons/CouponServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public CouponServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new CouponService();
this.service = new CouponService { Client = this.StripeClient };

this.createOptions = new CouponCreateOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public CreditNoteServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new CreditNoteService();
this.service = new CreditNoteService { Client = this.StripeClient };

this.createOptions = new CreditNoteCreateOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Customers/CustomerServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public CustomerServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new CustomerService();
this.service = new CustomerService { Client = this.StripeClient };

this.createOptions = new CustomerCreateOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Discounts/DiscountServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public DiscountServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new DiscountService();
this.service = new DiscountService { Client = this.StripeClient };
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Disputes/DisputeServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public DisputeServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new DisputeService();
this.service = new DisputeService { Client = this.StripeClient };

this.updateOptions = new DisputeUpdateOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public EphemeralKeyServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new EphemeralKeyService();
this.service = new EphemeralKeyService { Client = this.StripeClient };

this.createOptions = new EphemeralKeyCreateOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Events/EventServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public EventServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new EventService();
this.service = new EventService { Client = this.StripeClient };

this.listOptions = new EventListOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public ExchangeRateServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new ExchangeRateService();
this.service = new ExchangeRateService { Client = this.StripeClient };

this.listOptions = new ExchangeRateListOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public ExternalAccountServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new ExternalAccountService();
this.service = new ExternalAccountService { Client = this.StripeClient };

this.createOptions = new ExternalAccountCreateOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/FileLinks/FileLinkServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public FileLinkServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new FileLinkService();
this.service = new FileLinkService { Client = this.StripeClient };

this.createOptions = new FileLinkCreateOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Files/FileServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FileServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new FileService();
this.service = new FileService { Client = this.StripeClient };

this.createOptions = new FileCreateOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public InvoiceItemServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new InvoiceItemService();
this.service = new InvoiceItemService { Client = this.StripeClient };

this.createOptions = new InvoiceItemCreateOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/StripeTests/Services/Invoices/InvoiceServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public InvoiceServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new InvoiceService();
this.service = new InvoiceService { Client = this.StripeClient };

this.createOptions = new InvoiceCreateOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public AuthorizationServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new AuthorizationService();
this.service = new AuthorizationService { Client = this.StripeClient };

this.updateOptions = new AuthorizationUpdateOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public CardholderServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new CardholderService();
this.service = new CardholderService { Client = this.StripeClient };

this.createOptions = new CardholderCreateOptions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public IssuingCardServiceTest(
MockHttpClientFixture mockHttpClientFixture)
: base(stripeMockFixture, mockHttpClientFixture)
{
this.service = new CardService();
this.service = new CardService { Client = this.StripeClient };

this.createOptions = new CardCreateOptions
{
Expand Down
Loading

0 comments on commit 3762313

Please sign in to comment.