<p>What it does</p> Angular Karma Jasmine Error: Illegal state: Could not load the summary for directive

Now, new errors appears:

The pipe 'translate' could not be found ("<h1 class="section-title">{{[ERROR ->]'heroDetail' | translate}}</h1>
<md-progress-spinner *ngIf="!hero"
class="progre"): ng:///DynamicTestModule/HeroDetailComponent.html@0:28
Can't bind to 'color' since it isn't a known property of 'md-progress-spinner'.

And more... it's like all directives and components from angular material, and the pipe translate from ngx-translate/core do not appear to be included...

78357 次浏览

If you are concerned about shellcheck SC2046 (in which case you would receive a warning for the command docker stop $(docker ps -a -q)) and you are using Bash4+ (to be able to use the mapfile builtin command), you can run the following to stop all containers:

mapfile -t list < <(docker ps -q)
[[ ${#list[@]} -gt 0 ]] && docker container stop "${list[@]}"

You passed HeroDetailComponent to TestBed.createComponent() without declaring the component first:

TestBed.configureTestingModule({
imports: [AppModule,
CommonModule,
FormsModule,
SharedModule,
HeroRoutingModule,
ReactiveFormsModule
],
providers: [
{provide: APP_BASE_HREF, useValue: '/'}
],
declarations: [HeroDetailComponent]
}).compileComponents();

The reasoning:

    Hope it helps.


  • docker ps -q returns all active containers ids

  • Update for following errors in your test: Added some more imports (just take your HeroModule as a blueprint because that's basically what you want to import and provide).

  • mapfile stores the resulting container ids in the list array

  • [[ ${#list[@]} -gt 0 ]] tests if the list array has 1 or more elements to execute the next command

  • docker container stop "${list[@]}" stops all containers whose ids are stored in the listarray (will only run if the array has items)

  • Similarly, to remove all stopped containers:

    mapfile -t list < <(docker ps -aq)
    [[ ${#list[@]} -gt 0 ]] && docker container rm "${list[@]}"
    

    (docker ps -aq returns all container ids, even from stopped containers)

    you must import the component HeroDetailComponent in the right way. Notice that even case of letters is matter in paths. i.e ('@angular/forms' is correct, BUT'@angular/Forms' is not.

    You're missing the declarations, you need to add the class being tested into the declarations.

    declarations: [component]
    

    If you want to stop and remove all containers, you can run the above commands sequentially (first stop, then rm).

    This type of error raised due to missing adding component in declarations and services in provider of TestBed configuration.

    beforeEach(() => {
    TestBed.configureTestingModule({
    imports: [RouterTestingModule.withRoutes([
    { path: 'home', component: DummyComponent },
    { path: 'patients/find', component: DummyComponent }
    ])],
    declarations: [RoutingComponent, DummyComponent,BreadcrumbComponent],
    providers : [BreadCrumbService]
    });
    

    For clarity, the fix was similar to changing the folder name FOO to foo.

    To remove all Docker images:

    docker rmi $(docker images -aq)
    

    If you want to test a component without compiling it then you can by declaring it as a provider:

    beforeEach(() => {
    TestBed.configureTestingModule({
    // provide the component-under-test and dependent service
    providers: [
    WelcomeComponent,
    { provide: UserService, useClass: MockUserService }
    ]
    });
    // inject both the component and the dependent service.
    comp = TestBed.get(WelcomeComponent);
    userService = TestBed.get(UserService);
    });
    
    vider:

    beforeEach(() => {
    TestBed.configureTestingModule({
    // provide the component-under-test and dependent service
    providers: [
    WelcomeComponent,
    { provide: UserService, useClass: MockUserService }
    ]
    });
    // inject both the component and the dependent service.
    comp = TestBed.get(WelcomeComponent);
    userService = TestBed.get(UserService);
    });
    

    See: https://angular.io/guide/testing#component-test-basics

    Here is what I did:

    beforeAll(async(() => {
    TestBed.configureTestingModule({
    declarations: [BannerNotificationComponent]
    }).compileComponents()
    
    
    fixture = TestBed.createComponent(BannerNotificationComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    }));
    

    With the following code, I get a compile error C2065 'a': undeclared identifier (using visual studio 2017):

    [] {
    auto [a, b] = [] {return std::make_tuple(1, 2); }();
    auto r = [&] {return a; }(); //error C2065
    }();
    

    Using beforeAll fixes the issue. Hope this helps others as it took me about a day to get this resolve this obscure bug.

    However, the following code compiles:

    [] {
    int a, b;
    std::tie(a, b) = [] {return std::make_tuple(1, 2); }();
    auto r = [&] {return a; }();
    }();
    

    I imported the wrong module into the test suite! correcting which module was imported fixed this error.

    I thought that the two samples were equivalent. Is it a compiler bug or am I missing something ?

    Answer copied out of question

    TestBed.configureTestingModule({ imports: [

    The problem was that HeroesModule was not been imported anywhere. This works, because HeroesModule declares HeroDetailComponent, which was the initial problem:

    import {async, TestBed} from '@angular/core/testing';
    import {APP_BASE_HREF} from '@angular/common';
    import {AppModule} from '../../app.module';
    import {HeroDetailComponent} from './hero-detail.component';
    import {HeroesModule} from '../heroes.module';
    
    
    describe('HeroDetailComponent', () => {
    beforeEach(async(() => {
    TestBed.configureTestingModule({
    imports: [
    AppModule,
    HeroesModule
    ],
    providers: [
    {provide: APP_BASE_HREF, useValue: '/'}
    ],
    }).compileComponents();
    }));
    
    
    it('should create hero detail component', (() => {
    const fixture = TestBed.createComponent(HeroDetailComponent);
    const component = fixture.debugElement.componentInstance;
    expect(component).toBeTruthy();
    }));
    });