Skip to content

Commit 511ae3f

Browse files
Youssef1313rockfordlhotkaCopilot
authored
Avoid asserting in async void (#4673)
* Avoid asserting in async void * Update Source/Csla.test/BusyStatus/BusyStatusTests.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Revert copilot suggestion --------- Co-authored-by: Rockford Lhotka <rocky@lhotka.net> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 7fb9f3b commit 511ae3f

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

Source/Csla.test/BusyStatus/BusyStatusTests.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,25 +138,36 @@ public void ListTestNotBusy()
138138
}
139139

140140
[TestMethod]
141-
public void ListTestSaveWhileNotBusy()
141+
public async Task ListTestSaveWhileNotBusy()
142142
{
143143
IDataPortal<ItemWithAsynchRuleList> dataPortal = _noCloneOnUpdateDIContext.CreateDataPortal<ItemWithAsynchRuleList>();
144144

145145
ItemWithAsynchRuleList items = ItemWithAsynchRuleList.GetListWithItems(dataPortal);
146+
var tcs = new TaskCompletionSource();
146147
items[0].ValidationComplete += async (_, _) =>
147148
{
148-
#pragma warning disable MSTEST0040 // Do not assert inside 'async void' contexts
149-
Assert.IsFalse(items.IsBusy);
150-
Assert.IsTrue(items.IsSavable);
151-
items = await items.SaveAsync();
152-
string actual = items[0].OperationResult;
153-
Assert.AreEqual("DataPortal_Update", actual);
154-
#pragma warning restore MSTEST0040 // Do not assert inside 'async void' contexts
149+
try
150+
{
151+
Assert.IsFalse(items.IsBusy);
152+
Assert.IsTrue(items.IsSavable);
153+
items = await items.SaveAsync();
154+
string actual = items[0].OperationResult;
155+
Assert.AreEqual("DataPortal_Update", actual);
156+
}
157+
catch (Exception ex)
158+
{
159+
tcs.SetException(ex);
160+
}
161+
finally
162+
{
163+
tcs.TrySetResult();
164+
}
155165
};
156166

157167
items[0].RuleField = "some value";
158168
Assert.IsTrue(items.IsBusy);
159169
Assert.IsFalse(items.IsSavable);
170+
await tcs.Task;
160171
}
161172

162173
[TestMethod]
@@ -286,4 +297,4 @@ public async Task WaitForIdle_WhenReachingTheTimeoutATimeoutExceptionIsThrown()
286297
await item.Invoking(i => i.WaitForIdle(TimeSpan.FromMilliseconds(500))).Should().ThrowAsync<TimeoutException>();
287298
}
288299
}
289-
}
300+
}

0 commit comments

Comments
 (0)