Mock a component with named ref and injected service #1378
Replies: 1 comment
-
|
Sorry for being a bit late - while I do understand the issue, it depends (of course, as always :D ) what your target is. If <Foo>
<Bar/>
<Foo>You can do the following: ComponentFactories.AddStub<Bar>();This would remove We also offer an experimental source code generator, that does what is described above but also mimicks the public parameter surface of your component: https://bunit.dev/docs/extensions/bunit-generators.html?q=Stub |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It's mentioned in the documentation that when testing component
Foothat consumes a componentBarwhen the componentBarhas an@ref=barattribute, that we can write a subclassMockBaror use Moq as a substitute. That's great and works fine.But what should I do when
Barhas an injected service? Say,IWibbleService.The only way I can get it to work is by adding a
Mock<IWibbleService>().Objectto the DI container in theFootest. This feels like an anti-pattern in that my test ofFooshouldn't care about the dependencies ofBar.I've tried hacking various solutions including making the
IWibbleServicea virtual property ofBarand overriding it in a subclass but I'm not getting anywhere.Beta Was this translation helpful? Give feedback.
All reactions