[英]Unit testing a void method in an internal static class
我們如何在不實例化測試類中的對象的情況下測試void方法? 我了解在內部靜態類中,我們無法使用“新”來創建對象來對其進行測試。 我已經通過為非靜態類創建對象進行了單元測試,但是我沒有處理內部靜態類並且被卡住了。 任何指導都會很棒。
這是有問題的類/方法:
internal static class Util
{
public static void AssertBytesEqual(byte[] expected, byte[] actual)
{
if (expected.Length != actual.Length)
{
Debugger.Break();
throw new CryptographicException("The bytes were not of the expected length.");
}
for (int i = 0; i < expected.Length; i++)
{
if (expected[i] != actual[i])
{
Debugger.Break();
throw new CryptographicException("The bytes were not identical.");
}
}
}
}
您可以使用InternalsVisibileTo
屬性來允許另一個項目訪問內部類。
[assembly: InternalsVisibleTo("NameOfYourUnitTestProject")]
這將允許您的測試項目調用Util.AssertBytesEqual
。 通常將應用程序集級別的屬性放在AssemblyInfo.cs
文件中。
至於測試實際的方法本身,看起來是唯一的“輸出”,可以說是一個例外。 您只需測試該方法是否為各種輸入引發異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.