I try to test a call to method inside on a subscriber, but I can't. Always that I call to method that has a subscribe, console send to me the error "Error: Expected spy saveMug to have been called". Also fail with reContent method.
My test should be isolated, so I can´t use fixture or testbed.
This is my component code
@Component({
selector: "app-mug",
templateUrl: "./mug.component.html",
styleUrls: ["./mug.component.scss"],
})
export class MugComponent implements OnInit
{
public mug: Mug= new Mug();
constructor(
public constants: Constants,
public mugService: mugService,
private messageService: MessageService,
public route: ActivatedRoute,
public router: Router,
) {
super(constants, route);
}
ngOnInit() {}
public saveMug(previous?: boolean): void {
const sub = this.mugService.saveMug(this.mug).subscribe((mug) => {
if (previous === undefined || previous === false) {
this.messageService.showMessage(
this.constants.MUG,
[this.constants.MUG_OK]
);
} else {
this.messageService.showMessage(
this.constants.MUG,
[this.constants.NO_MUG]
);
}
this.mug= new Mug(mug.dates);
});
this.subscriptions.add(sub);
}
}
This is my Spec.ts code
describe("MugComponent", () => {
let component;
const constants: Constants = new Constants();
let messageService= jasmine.createSpyObj("messageService", ["showMessage"]);
let route;
let router: Router;
beforeEach(() => {
component = new MugComponent(
constants,
messageService,
route,
router
);
component.mug= new Mug();
});
it("should call to showMessage", () => {
spyOn(component, "showMessage");
component.saveMug(true);
expect(component.showMessage).toHaveBeenCalled();
});
});
This is the Error
Error: Expected spy showMessage to have been called. at
Thanks.