Nullinjectorerror: no provider for FormBuilder (I’m importing ReactiveFormsModule)

Open app.component.spec.ts and add ReactiveFormsModule to imports array. app.component.spec.ts describe(‘AppComponent’, () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ ReactiveFormsModule <================================= add this ], declarations: [ AppComponent, HoursCalculatorComponent ], }).compileComponents(); }));

Calling beforeEach and afterEach with nested describe blocks

To accomplish this I define a common function and then refer to it in the beforeAll/afterAll of each nested describe. describe(‘Wrapper’, function() { var _startup = function(done) { login(); window.setTimeout(done, 150); }; var _shutdown = function() { logout(); }; describe(‘Inner 1’, function() { beforeAll(_startup); afterAll(_shutdown); }); describe(‘Inner 2’, function() { beforeAll(_startup); afterAll(_shutdown); }); describe(‘Inner 3’, … Read more

Updating the version of Jasmine used in karma-jasmine

You can know the jasmine version you are using by running the following Spec: describe(‘Test to print out jasmine version’, function() { it(‘prints jasmine version’, function() { console.log(‘jasmine-version:’); console.log(jasmine.version || (jasmine.getEnv().versionString && jasmine.getEnv().versionString())); }); }); and then checking the karma output in your console or browser. It should be something like: LOG: ‘jasmine-version:’ LOG: ‘2.3.4’ … Read more

Angular testing how to prevent ngOnInit call to test a method directly

Preventing lifecycle hook (ngOnInit) from being called is a wrong direction. The problem has two possible causes. Either the test isn’t isolated enough, or testing strategy is wrong. Angular guide is quite specific and opinionated on test isolation: However, it’s often more productive to explore the inner logic of application classes with isolated unit tests … Read more

Mocking $httpBackend – how to handle “Unexpected request, No more request expected”?

Your test fails because a request is made which you haven’t specified. Try to add: $httpBackend.when(‘GET’, ‘views/core/main/main.html’).respond(fakedMainResponse); Of course you should also define fakedMainResponse. Please take a look also at the documentation (section Request Expectations vs Backend Definitions) which says: Request expectations provide a way to make assertions about requests made by the application and … Read more

Unable to simulate keypress event in Angular 2 unit test (Jasmine)

I’ve had some trouble simulating a keypress in a unit test also. But came across an answer by Seyed Jalal Hosseini. It might be what you’re after. If you’re attempting to simulate a keypress you can trigger an event by calling dispatchEvent(new Event(‘keypress’)); on the element. Here is the answer I’m referring to which gives … Read more

Angular – unit test for a subscribe function in a component

You need this for version rxjs@6 and above. For older rxjs version answer is below: import { of } from ‘rxjs’; it(“should call getUsers and return list of users”, async(() => { const response: User[] = []; spyOn(userService, ‘getUsers’).and.returnValue(of(response)) homeComponent.getUsers(); fixture.detectChanges(); expect(homeComponent.listOfUsers).toEqual(response); })); For old rxjs version change import from: import { of } from … Read more

Getting “$digest already in progress” in async test with Jasmine 2.0

$httpBacked.flush() actually starts and completes a $digest() cycle. I spent all day yesterday digging into the source of ngResource and angular-mocks to get to the bottom of this, and still don’t fully understand it. As far as I can tell, the purpose of $httpBackend.flush() is to avoid the async structure above entirely. In other words, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)