-
-
Notifications
You must be signed in to change notification settings - Fork 119
Document testing components that use native dependencies #511
Copy link
Copy link
Open
Labels
documentationImprovements or additions to documentationImprovements or additions to documentationgood first issueGood for newcomersGood for newcomers
Description
Document the how native dependencies will work in a bUnit context.
Original issue text:
Components that use native dependencies cannot be rendered with bUnit.
Simple example (lifted from here):
Let’s add a simple native C function to a Blazor WebAssembly app:
- Run
dotnet workload install wasm-tools - Create a new Blazor WebAssembly project.
- Add a Test.c file to the project.
- Add a C function in Test.c for computing factorials:
int fact(int n)
{
if (n == 0) return 1;
return n * fact(n - 1);
} - Add a NativeFileReference for Test.c in your project file:
<ItemGroup>
<NativeFileReference Include="Test.c" />
</ItemGroup>- In Pages/Index.razor add a DllImport for the fact function in generated Test library:
@using System.Runtime.InteropServices
...razor
@code {
[DllImport("Test")]
static extern int fact(int n);
}- Call fact method from your .NET code.
<p>@fact(3)</p>- Add a xunit project and add bUnit to it
- Create a simple test that renders the Index component:
public class UnitTest1 : TestContext
{
[Fact]
public void Test1()
{
var cut = RenderComponent<Index>();
}
}- Doing a
dotnet testyields the following error message:
System.DllNotFoundException : Unable to load DLL 'Test' or one of its dependencies: The specified module could not be found. (0x8007007E)
Options 1 - dont support it
For now, I am content with just telling users to wrap their extern calls in an abstract that they inject into the components that needs it, and then mock/fake that during testing.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
documentationImprovements or additions to documentationImprovements or additions to documentationgood first issueGood for newcomersGood for newcomers