Adding / Mocking JsFunctionalityService & IJSRuntime #1141
-
I am trying to add a JsFunctionalityService class as scoped to my Services in my test class. The constructor and fields of JsFunctionalityService look like this: I have also added it to my Startup.cs like this: When I am trying to write a test for a NavigationFrame component that has JsFunctionalityService injected using the below code, I am getting an error saying that my argument type in the test function is not assignable to the parameter in the test function. I do not need JsFunctionalityService for anything (ie. I am not testing that itself), but I cannot run any tests because the NavigationFrame component that I want to write tests for cannot be rendered as bUnit says that "cannot register JsFunctionalityService as a service". I have tried mocking JsFunctionalityService and IJSRuntime without success. Could someone guide me as to what is going on here please and how I could register JsFunctionalityService as a service to write tests for the NavigationFrame component that I need to test. I would really appreciate it. Many thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hey @tenngs, there are multiple ways you could achieve that. The first and easiest, do the same as in your production code: Services.AddScoped<IJsFunctionalityService, JsFunctionalityService>(); No reason to new up the instance yourself - you have a container for this. Also the But I would suggest (because you said you do not need the service per see): Services.AddScoped(_ => Mock.Of<IJsFunctionalityService>()); // Use your mocking framework |
Beta Was this translation helpful? Give feedback.
Ah. Yes - your component should either use the interface version or register the instance.
So in your component do this:
Or register inside your bUnit test the instance:
Edit: In your test, you are only registering the interface, but the component is asking for the instance.