Comments (7)
Here's an example I whipped up really quick:
https://stackblitz.com/edit/bah?embed=1&file=app/hello.component.spec.ts
The meat of it is:
providers: [
{
provide: AppComponent,
useClass: MockComponent(AppComponent)
}
]
from ng-mocks.
I didn't know you could provide components, that's cool. But i modified your code to be closer to mine and it still didn't work.
The main difference is my "hello" component is projected content. So i move it in the tests and made a testComponent in the spec file.
@Component({ template: `
<my-app>
<hello>
</hello>
</my-app>
` })
class TestComponent {}
It seems like it might be when projection is involved that the problem arises?
Here is the modified one, i added a new spec file as well that is identical except is just removed MockComponent and those tests work.
https://stackblitz.com/edit/bah-imogy4?embed=1&file=app/hello.component.spec.ts
from ng-mocks.
@kenborge looks like the issue here is related to the TestComponent
approach combined with injection of a mock parent. I'm not sure why TestBed doesn't care for the approach. I would recommend avoiding the TestComponent
"layer" in this case and stick to just TestBed.createComponent(HelloComponent)
. It'll give you more control over your component inputs too so it's kind-of a double-win.
@ike18t showed that you can provide components in the providers
block, you may also try using TestBed.overrideComponent
to do this too. Not sure if there is a way to get TestBed to play nice with this approach, but it's worth mentioning. I must say though, @ike18t's suggestion seems to be much more readable and pragmatic than this using overrideComponent
.
TestBed.configureTestingModule({
declarations: [ HelloComponent ],
});
TestBed.overrideComponent(HelloComponent, {
set: {
viewProviders: [
{provide: AppComponent, useClass: MockComponent(AppComponent)}
]
}
});
I got that test running in this StackBlitz project, it's very similar to the one @ike18t provided, just using the different injection technique.
from ng-mocks.
@getsaf Creating a TestComponent does have some pretty valid use cases. Like if you want to test projected content. So you need to compile something like:
@Component({ template: `
<hello>
PROJECTED CONTENT
</hello>
` })
class TestComponent {}
It does seem like this is possible with the viewProviders approach though. It doesn't work using normal provide like @ike18t mentioned, but overriding viewProviders seems to be a solution.
https://stackblitz.com/edit/bah-yfg6d8?embed=1&file=app/hello.component.spec.ts
from ng-mocks.
@kenborge Did that solve your problem? Is it cool if I close this issue?
from ng-mocks.
Closing unless there is a reason not to. Feel free to reopen if this is still an issue.
from ng-mocks.
@ike18t yeah it did work using the viewProviders approach. I would still consider it a bug though or at least a limitation of the mock framework that maybe should be documented.
from ng-mocks.
Related Issues (20)
- doc: better example of `ActivatedRoute.params` HOT 2
- Feature Request: MockBuilder strict mode with standalone components
- docs: how to mock with `MockBuilder.provide`
- support vitest
- ngMocksUniverse.global
- Bug: MockMiddleware{Index} HOT 1
- update links in the repo HOT 2
- update sponsor links in the repo HOT 2
- Bug: cannot set return value of DI'ed pipe HOT 1
- Bug: `.keep(ReactiveFormsModule)` does not actually keep HOT 3
- Bug: Angular 17.1.0 `input.required()` issues HOT 14
- Feature Request: Documentation for functional route guards without the use of NgModule
- Bug: Injection of mocked service fails for nested service ... but only sometimes ...
- Bug: cannot `.replace()` a component that has an abstract base class
- Bug: formatHtml on MockedComponentFixture / DebugElement won't give expected value
- Bug: try to avoid recursive parsing when a standalone component imports itself
- Bug: Incompatibility of MockComponent with new viewChild signal function HOT 7
- Bug: MockComponent (Standalone) mocks all components not just the ones specified HOT 3
- Bug: MockRender throws an error on mat-tab HOT 2
- MockComponent function set Signal Inputs as Decorator Inputs
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ng-mocks.