-
-
Notifications
You must be signed in to change notification settings - Fork 113
Looking for how to create unit test with c# when the component creates a canvas (BECanvasComponent) #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @sakvin1 It looks as if the I do not know the library you are using, so I am not sure what JSRuntime calls you need to mock to make the test pass. Maybe @galvesribeiro can help here. |
Thank you @egil for your quick response, Is there any way if I can create a context in the unit test, and then inject to the component? That would work. Since it failed to create the context when rendering the component in unit test. Canvas2DContext context = await _canvasReference.CreateCanvas2DAsync(); |
@sakvin1 there are two general ways of injecting dependencies into components in Blazor, through services and through cascading values. In both cases, the easiest way is if the dependencies are represented in the components that use them as interfaces (abstractions). Then it is easy to use a framework like Moq to mock these, and inject the mock, either through the The problem is that The best solution I can come up with is for you to create an adapter around Canvas2DContext that allows you to mock it in tests and use the real in production. That is a bit of work, but will let you test your code that uses it in isolation. The alternative is to mock the |
I'll close this issue. If you still need help, just comment again. |
Hi,
I encounter an issue when I tried to create a unit test for a component that creating a canvas. Unit Test failed with this message.
Message:
Test method TestCanvas threw exception:
System.AggregateException: One or more errors occurred. (Object reference not set to an instance of an object.) ---> System.NullReferenceException: Object reference not set to an instance of an object.
IndexComponent.OnAfterRenderAsync(Boolean firstRender) line 24
This is the line that causes an error in Index.razor.cs.
Basically, I can't create a JS mock for Blazor.Extensions.Canvas.Canvas2D. Following are the code.
In Index.razor
In Index.razor.cs
In Test.cs
Please help.
The text was updated successfully, but these errors were encountered: