When running a Blazor app in the browser, many .NET methods will throw a PlatformNotSupportedException
, for example, TcpClient.ConnectAsync()
. This is fine, but I want to run unit tests that will catch these types of restrictions.
I have found that when running bUnit tests (within a default bUnit project), that executing TcpClient.ConnectAsync()
does not throw an exception. This is not what I want; I don't want to be developing services that may call APIs that will pass at test-time but fail at run-time.
How can I run unit tests that intentionally have the same restrictions that would exist in an app running in the browser?
bUnit is platform agnostic. You can use it to test components that are used by Blazor Server, Wasm, Maui or some other runtime.
So, in the context of a bUnit test, it may not make sense to test for such things.
In general, according to this, you cannot override the platform you are running under: How to run specific unit tests only when running on a specific operating system?, so your best bet is to perform smoke tests with e.g. Playwright instead, which uses a real browser for testing.