Code Monkey home page Code Monkey logo

Comments (7)

ike18t avatar ike18t commented on May 12, 2024 1

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.

kenjiqq avatar kenjiqq commented on May 12, 2024

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.

getsaf avatar getsaf commented on May 12, 2024

@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.

kenjiqq avatar kenjiqq commented on May 12, 2024

@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.

ike18t avatar ike18t commented on May 12, 2024

@kenborge Did that solve your problem? Is it cool if I close this issue?

from ng-mocks.

ike18t avatar ike18t commented on May 12, 2024

Closing unless there is a reason not to. Feel free to reopen if this is still an issue.

from ng-mocks.

kenjiqq avatar kenjiqq commented on May 12, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.