Skip to content

Commit

Permalink
remove all usages of TaskFactory in test suite (#2164)
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftkey authored Mar 31, 2020
1 parent b540913 commit 4b0e725
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 179 deletions.
29 changes: 15 additions & 14 deletions Octokit.Tests/Clients/AssigneesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,10 @@ public class TheCheckAssigneeMethod
[InlineData(HttpStatusCode.NotFound, false)]
public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected)
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(status, null, new Dictionary<string, string>(), "application/json")));
var responseTask = TestSetup.GetApiResponse(status);
var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "repos/foo/bar/assignees/cody"),
null, null).Returns(response);
null, null).Returns(responseTask);
var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new AssigneesClient(apiConnection);
Expand All @@ -133,11 +132,12 @@ public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool
[InlineData(HttpStatusCode.NotFound, false)]
public async Task RequestsCorrectValueForStatusCodeWithRepositoryId(HttpStatusCode status, bool expected)
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(status, null, new Dictionary<string, string>(), "application/json")));
var responseTask = TestSetup.GetApiResponse(status);

var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/assignees/cody"),
null, null).Returns(response);
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/assignees/cody"), null, null)
.Returns(responseTask);

var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new AssigneesClient(apiConnection);
Expand All @@ -150,11 +150,12 @@ public async Task RequestsCorrectValueForStatusCodeWithRepositoryId(HttpStatusCo
[Fact]
public async Task ThrowsExceptionForInvalidStatusCode()
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json")));
var responseTask = TestSetup.GetApiResponse(HttpStatusCode.Conflict);

var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "repos/foo/bar/assignees/cody"),
null, null).Returns(response);
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "repos/foo/bar/assignees/cody"), null, null)
.Returns(responseTask);

var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new AssigneesClient(apiConnection);
Expand All @@ -165,11 +166,11 @@ public async Task ThrowsExceptionForInvalidStatusCode()
[Fact]
public async Task ThrowsExceptionForInvalidStatusCodeWithRepositoryId()
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json")));
var response = new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json");
var responseTask = Task.FromResult<IApiResponse<object>>(new ApiResponse<object>(response));
var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "repositories/1/assignees/cody"),
null, null).Returns(response);
null, null).Returns(responseTask);
var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new AssigneesClient(apiConnection);
Expand Down
10 changes: 5 additions & 5 deletions Octokit.Tests/Clients/AuthorizationsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ public async Task UsesCallbackToRetrieveTwoFactorCode()
"secret",
Arg.Any<NewAuthorization>(),
"two-factor-code")
.Returns(Task.Factory.StartNew(() => new ApplicationAuthorization(0, null, null, null, null, null, null, null, DateTimeOffset.Now, DateTimeOffset.Now, null, "xyz")));
.Returns(Task.FromResult(new ApplicationAuthorization(0, null, null, null, null, null, null, null, DateTimeOffset.Now, DateTimeOffset.Now, null, "xyz")));

var result = await client.GetOrCreateApplicationAuthentication("clientId",
"secret",
data,
e => Task.Factory.StartNew(() => twoFactorChallengeResult));
e => Task.FromResult(twoFactorChallengeResult));

client.Received().GetOrCreateApplicationAuthentication("clientId",
"secret",
Expand All @@ -205,12 +205,12 @@ public async Task RetriesWhenResendRequested()
"secret",
Arg.Any<NewAuthorization>(),
"two-factor-code")
.Returns(Task.Factory.StartNew(() => new ApplicationAuthorization(0, null, null, null, null, null, null, null, DateTimeOffset.Now, DateTimeOffset.Now, null, "OAUTHSECRET")));
.Returns(Task.FromResult(new ApplicationAuthorization(0, null, null, null, null, null, null, null, DateTimeOffset.Now, DateTimeOffset.Now, null, "OAUTHSECRET")));

var result = await client.GetOrCreateApplicationAuthentication("clientId",
"secret",
data,
e => Task.Factory.StartNew(() => challengeResults.Dequeue()));
e => Task.FromResult(challengeResults.Dequeue()));

client.Received(2).GetOrCreateApplicationAuthentication("clientId",
"secret",
Expand Down Expand Up @@ -245,7 +245,7 @@ public async Task ThrowsTwoFactorChallengeFailedExceptionWhenProvidedCodeIsIncor
"clientId",
"secret",
data,
e => Task.Factory.StartNew(() => challengeResults.Dequeue())));
e => Task.FromResult(challengeResults.Dequeue())));

Assert.NotNull(exception);
client.Received().GetOrCreateApplicationAuthentication("clientId",
Expand Down
46 changes: 25 additions & 21 deletions Octokit.Tests/Clients/FollowersClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace Octokit.Tests.Clients
{
/// <summary>
/// Client tests mostly just need to make sure they call the IApiConnection with the correct
/// Client tests mostly just need to make sure they call the IApiConnection with the correct
/// relative Uri. No need to fake up the response. All *those* tests are in ApiConnectionTests.cs.
/// </summary>
public class FollowersClientTests
Expand Down Expand Up @@ -204,11 +204,11 @@ public class TheIsFollowingForCurrentMethod
[InlineData(HttpStatusCode.NotFound, false)]
public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected)
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(status, null, new Dictionary<string, string>(), "application/json")));
var response = new Response(status, null, new Dictionary<string, string>(), "application/json");
var responseTask = Task.FromResult<IApiResponse<object>>(new ApiResponse<object>(response));
var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "user/following/alfhenrik"),
null, null).Returns(response);
null, null).Returns(responseTask);
var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new FollowersClient(apiConnection);
Expand All @@ -221,11 +221,11 @@ public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool
[Fact]
public async Task ThrowsExceptionForInvalidStatusCode()
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json")));
var response = new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json");
var responseTask = Task.FromResult<IApiResponse<object>>(new ApiResponse<object>(response));
var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "user/following/alfhenrik"),
null, null).Returns(response);
null, null).Returns(responseTask);
var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new FollowersClient(apiConnection);
Expand All @@ -251,11 +251,11 @@ public class TheIsFollowingMethod
[InlineData(HttpStatusCode.NotFound, false)]
public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected)
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(status, null, new Dictionary<string, string>(), "application/json")));
var response = new Response(status, null, new Dictionary<string, string>(), "application/json");
var responseTask = Task.FromResult<IApiResponse<object>>(new ApiResponse<object>(response));
var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "users/alfhenrik/following/alfhenrik-test"),
null, null).Returns(response);
null, null).Returns(responseTask);
var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new FollowersClient(apiConnection);
Expand All @@ -268,11 +268,11 @@ public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool
[Fact]
public async Task ThrowsExceptionForInvalidStatusCode()
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json")));
var response = new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json");
var responseTask = Task.FromResult<IApiResponse<object>>(new ApiResponse<object>(response));
var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "users/alfhenrik/following/alfhenrik-test"),
null, null).Returns(response);
null, null).Returns(responseTask);
var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new FollowersClient(apiConnection);
Expand All @@ -299,11 +299,13 @@ public class TheFollowMethod
[InlineData(HttpStatusCode.NoContent, true)]
public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected)
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(status, null, new Dictionary<string, string>(), "application/json")));
var response = new Response(status, null, new Dictionary<string, string>(), "application/json");
var responseTask = Task.FromResult<IApiResponse<object>>(new ApiResponse<object>(response));

var connection = Substitute.For<IConnection>();
connection.Put<object>(Arg.Is<Uri>(u => u.ToString() == "user/following/alfhenrik"),
Args.Object).Returns(response);
connection.Put<object>(Arg.Is<Uri>(u => u.ToString() == "user/following/alfhenrik"), Args.Object)
.Returns(responseTask);

var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new FollowersClient(apiConnection);
Expand All @@ -316,11 +318,13 @@ public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool
[Fact]
public async Task ThrowsExceptionForInvalidStatusCode()
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json")));
var response = new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json");
var responseTask = Task.FromResult<IApiResponse<object>>(new ApiResponse<object>(response));

var connection = Substitute.For<IConnection>();
connection.Put<object>(Arg.Is<Uri>(u => u.ToString() == "user/following/alfhenrik"),
new { }).Returns(response);
connection.Put<object>(Arg.Is<Uri>(u => u.ToString() == "user/following/alfhenrik"), Args.Object)
.Returns(responseTask);

var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new FollowersClient(apiConnection);
Expand Down
22 changes: 13 additions & 9 deletions Octokit.Tests/Clients/GistsClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,13 @@ public void RequestsCorrectUnstarUrl()
[InlineData(HttpStatusCode.NotFound, false)]
public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool expected)
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(status, null, new Dictionary<string, string>(), "application/json")));
var response = new Response(status, null, new Dictionary<string, string>(), "application/json");
var responseTask = Task.FromResult<IApiResponse<object>>(new ApiResponse<object>(response));

var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"),
null, null).Returns(response);
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"), null, null)
.Returns(responseTask);

var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);
var client = new GistsClient(apiConnection);
Expand All @@ -516,11 +518,13 @@ public async Task RequestsCorrectValueForStatusCode(HttpStatusCode status, bool
[Fact]
public async Task ThrowsExceptionForInvalidStatusCode()
{
var response = Task.Factory.StartNew<IApiResponse<object>>(() =>
new ApiResponse<object>(new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json")));
var response = new Response(HttpStatusCode.Conflict, null, new Dictionary<string, string>(), "application/json");
var responseTask = Task.FromResult<IApiResponse<object>>(new ApiResponse<object>(response));

var connection = Substitute.For<IConnection>();
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"),
null, null).Returns(response);
connection.Get<object>(Arg.Is<Uri>(u => u.ToString() == "gists/1/star"), null, null)
.Returns(responseTask);

var apiConnection = Substitute.For<IApiConnection>();
apiConnection.Connection.Returns(connection);

Expand Down Expand Up @@ -568,4 +572,4 @@ public void PostsToTheCorrectUrl()
connection.Received().Patch<Gist>(Arg.Is<Uri>(u => u.ToString() == "gists/1"), Arg.Any<object>());
}
}
}
}
Loading

0 comments on commit 4b0e725

Please sign in to comment.