Skip to content

Commit 41d44d5

Browse files
authored
test: Isolate network calls in RelationTests and UserTests (#428)
1 parent 22803e5 commit 41d44d5

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

‎Parse.Tests/RelationTests.cs‎

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,11 @@ private class Friend : ParseObject { }
3131
[TestInitialize]
3232
public void SetUp()
3333
{
34-
// Initialize the client and ensure the instance is set
35-
Client = new ParseClient(new ServerConnectionData { Test = true });
36-
Client.Publicize();
37-
38-
// Register the test classes
39-
Client.RegisterSubclass(typeof(TestObject));
40-
Client.RegisterSubclass(typeof(Friend));
41-
Client.RegisterSubclass(typeof(ParseUser));
42-
Client.RegisterSubclass(typeof(ParseSession));
43-
Client.RegisterSubclass(typeof(ParseUser));
44-
4534
// **--- Mocking Setup ---**
35+
// Build a service hub with mocked controllers so the tests never make real
36+
// network requests (previously the mock hub was created but never wired into
37+
// the client, so calls such as SignUpAsync hit the dead https://api.parse.com
38+
// default server and failed on the CI runner).
4639
var hub = new MutableServiceHub(); // Use MutableServiceHub for mocking
4740
var mockUserController = new Mock<IParseUserController>();
4841
var mockObjectController = new Mock<IParseObjectController>();
@@ -52,7 +45,7 @@ public void SetUp()
5245
.Setup(controller => controller.SignUpAsync(
5346
It.IsAny<IObjectState>(),
5447
It.IsAny<IDictionary<string, IParseFieldOperation>>(),
55-
It.IsAny<IServiceHub>(),
48+
It.IsAny<IServiceHub>(),
5649
It.IsAny<CancellationToken>()))
5750
.ReturnsAsync(new MutableObjectState { ObjectId = "some0neTol4v4" }); // Predefined ObjectId for User
5851

@@ -74,10 +67,19 @@ public void SetUp()
7467
hub.UserController = mockUserController.Object;
7568
hub.ObjectController = mockObjectController.Object;
7669

70+
// Initialize the client with the mocked hub and ensure the instance is set.
71+
Client = new ParseClient(new ServerConnectionData { Test = true }, hub);
72+
Client.Publicize();
73+
74+
// Register the test classes
75+
Client.RegisterSubclass(typeof(TestObject));
76+
Client.RegisterSubclass(typeof(Friend));
77+
Client.RegisterSubclass(typeof(ParseUser));
78+
Client.RegisterSubclass(typeof(ParseSession));
7779
}
78-
80+
7981
[TestCleanup]
80-
public void TearDown() => (Client.Services as ServiceHub).Reset();
82+
public void TearDown() => (Client.Services as ServiceHub ?? (Client.Services as OrchestrationServiceHub)?.Default as ServiceHub)?.Reset();
8183

8284
[TestMethod]
8385
public void TestRelationQuery()

‎Parse.Tests/UserTests.cs‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ public async Task TestLogOut()
162162
.Setup(obj => obj.ClearFromDiskAsync())
163163
.Returns(Task.CompletedTask);
164164

165-
// Mock LogOutAsync to ensure it can execute its logic
165+
// Mock LogOutAsync to complete successfully. (CallBase() cannot be used here because
166+
// IParseCurrentUserController is an interface and has no base implementation to proceed to.)
166167
mockCurrentUserController
167168
.Setup(obj => obj.LogOutAsync(It.IsAny<IServiceHub>(), It.IsAny<CancellationToken>()))
168-
.CallBase(); // Use the actual LogOutAsync implementation
169+
.Returns(Task.CompletedTask);
169170

170171
// Mock SessionController for session revocation
171172
var mockSessionController = new Mock<IParseSessionController>();
@@ -183,6 +184,10 @@ public async Task TestLogOut()
183184
// Inject mocks into ParseClient
184185
var client = new ParseClient(new ServerConnectionData { Test = true }, hub);
185186

187+
// Bind the user to the mocked client so session revocation during logout uses the
188+
// mocked SessionController instead of making a real network request.
189+
user.Bind(client);
190+
186191
// Act: Perform logout
187192
await client.LogOutAsync(CancellationToken.None);
188193

0 commit comments

Comments
 (0)