Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
qdraw committed Sep 11, 2024
1 parent a2e59d1 commit 6ddadd5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,6 @@ public async Task SearchService_Search_Percentage()


[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public async Task SearchService_Search_ToLong_ArgumentException()
{
const string longTestText =
Expand Down Expand Up @@ -1084,7 +1083,7 @@ public async Task SearchService_Search_ToLong_ArgumentException()
" elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Lorem ipsum dolor sit" +
" amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna ";

await _search.Search(longTestText);
await Assert.ThrowsExceptionAsync<ArgumentException>(() => _search.Search(longTestText));
// Expect ArgumentException
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,46 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using starsky.foundation.storage.Exceptions;

namespace starskytest.starsky.foundation.storage.Exceptions
namespace starskytest.starsky.foundation.storage.Exceptions;

[TestClass]
public sealed class DecodingExceptionTest
{
[TestClass]
public sealed class DecodingExceptionTest
[TestMethod]
public void DecodingException()
{
[TestMethod]
[ExpectedException(typeof(DecodingException))]
public void DecodingException()
{
// Arrange
#pragma warning disable SYSLIB0050
var info = new SerializationInfo(typeof(Exception),
new FormatterConverter());
info.AddValue("Message", "");
info.AddValue("InnerException", new Exception());
info.AddValue("HelpURL", "");
info.AddValue("StackTraceString", "");
info.AddValue("RemoteStackTraceString", "");
info.AddValue("HResult", 1);
info.AddValue("Source", "");

var ctor =
typeof(DecodingException).GetConstructors(BindingFlags.Instance |
BindingFlags.NonPublic | BindingFlags.InvokeMethod).FirstOrDefault();
var instance =
( DecodingException? ) ctor?.Invoke(new object[]
{
info,
new StreamingContext(StreamingContextStates.All)
});

throw instance!;
var info = new SerializationInfo(typeof(Exception), new FormatterConverter());
#pragma warning restore SYSLIB0050
info.AddValue("Message", "");
info.AddValue("InnerException", new Exception());
info.AddValue("HelpURL", "");
info.AddValue("StackTraceString", "");
info.AddValue("RemoteStackTraceString", "");
info.AddValue("HResult", 1);
info.AddValue("Source", "");

var ctor = typeof(DecodingException).GetConstructors(BindingFlags.Instance |
BindingFlags.NonPublic)
.FirstOrDefault();

if (ctor == null)
{
Assert.Fail("No suitable constructor found for DecodingException.");
}

// Act & Assert
var ex = Assert.ThrowsException<DecodingException>(() =>
{
var instance = (DecodingException)ctor.Invoke(new object[]
{
info, new StreamingContext(StreamingContextStates.All)
});
throw instance;
});

// Optionally verify the exception message or other properties
Assert.IsTrue(ex.ToString().Contains("System.Exception"));
}
}

0 comments on commit 6ddadd5

Please sign in to comment.