M.D. Yaswanth
M.D. Yaswanth

Reputation: 11

getting error in directive.spec.ts - angular

I created a directive called changeText and in file change-text.directive.spec.ts. I get and error in

const directive = new ChangeTextDirective();

change-text.directive.spec.ts

import { ChangeTextDirective } from './change-text.directive';

describe('ChangeTextDirective', () => 
{
  it('should create an instance', () => {
    const directive = new ChangeTextDirective();  //error here is asking for an argument
    expect(directive).toBeTruthy();
  });
});

change-text.directive.ts

import { Directive, ElementRef } from '@angular/core';

@Directive({
  selector: '[changeText]'
})
export class ChangeTextDirective {
  constructor(Element : ElementRef) {
    console.log(Element);
    Element.nativeElement.innerText="Has been changed :)";
   }
}

HTML:

<div style="text-align:center;">
  <span changeText>Can You Change Me</span>
</div>

Upvotes: 1

Views: 446

Answers (1)

Eugene
Eugene

Reputation: 1517

If it asks for an argument, give it an argument! =)

Use inject:

it('should create an instance', inject([], (elementRef: ElementRef) => {
  const directive = new ChangeTextDirective(elementRef);
  expect(directive).toBeTruthy();
}));

Upvotes: 3

Related Questions