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.
-
It's mentioned in the documentation that when testing component
Foo
that consumes a componentBar
when the componentBar
has an@ref=bar
attribute, that we can write a subclassMockBar
or use Moq as a substitute. That's great and works fine.But what should I do when
Bar
has an injected service? Say,IWibbleService
.The only way I can get it to work is by adding a
Mock<IWibbleService>().Object
to the DI container in theFoo
test. This feels like an anti-pattern in that my test ofFoo
shouldn't care about the dependencies ofBar
.I've tried hacking various solutions including making the
IWibbleService
a virtual property ofBar
and overriding it in a subclass but I'm not getting anywhere.Beta Was this translation helpful? Give feedback.
All reactions