[英]Test the call to method into a subscribe on Jamine (Isolated Test)
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".
我总是调用具有订阅的方法,控制台向我发送错误“错误:预期的间谍 saveMug 已被调用”。 Also fail with reContent method.
reContent 方法也失败了。
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这是我的 Spec.ts 代码
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.
错误:预期的间谍 showMessage 已被调用。 at
在
Thanks.谢谢。
What is mugService
?什么是
mugService
? How do you have access to it without injecting it in the constructor
?如果不将其注入
constructor
中,您如何访问它?
That being said, I think you may need to use fakeAsync/tick
to make the subscription happen before your assertion.话虽如此,我认为您可能需要在断言之前使用
fakeAsync/tick
来进行订阅。
In the component, I am going to assume there is a MugService
now.在组件中,我假设现在有一个
MugService
。
constructor(
public constants: Constants,
private messageService: MessageService,
private mugService: MugService,
public route: ActivatedRoute,
public router: Router,
) {
super(constants, route);
}
let component;
const constants: Constants = new Constants();
let messageService= jasmine.createSpyObj("messageService", ["showMessage"]);
// !! create a spy
let mugService = jasmine.createSpyObj<MugService>("MugService", ['saveMug']);
let route;
let router: Router;
beforeEach(() => {
component = new MugComponent(
constants,
messageService,
// !! give spy object to component
mugService,
route,
router
);
component.mug= new Mug();
});
// !! wrap in fakeAsync
it("should call to showMessage", fakeAsync(() => {
// !! I don't think component.showMessage exists, messageService.showMessge does and it is already spied on
// !! make mugService.saveMug and return an observable
mugService.saveMug.and.returnValue(of({})); // you can mock what it returns however you wish
component.saveMug(true);
// !! wait for the subscription to complete
tick();
expect(messageService.showMessage).toHaveBeenCalled();
});
}));
Well, I would suggest to let the mocked messageService's saveMenu
method return a value like of(new Mug())
.好吧,我建议让被模拟的 messageService 的
saveMenu
方法返回一个类似of(new Mug())
的值。
I assume, the test case should validate, whether the messageService got called correctly after the component's saveMug
method was executed.我假设,测试用例应该验证 messageService 在组件的
saveMug
方法执行后是否被正确调用。 This could be accomplished by doing something similar like:这可以通过执行类似的操作来完成:
describe("MugComponent", () => {
let component: MugComponent;
const constants: Constants = new Constants();
let route;
let router: Router;
let messageService= jasmine.createSpyObj("messageService", ["showMessage", "saveMenu"]);
messageService.saveMenu.and.returnValue(of(new Mug()));
beforeEach(() => {
component = new MugComponent(
constants,
messageService,
route,
router
);
component.mug = new Mug();
});
it("should call to showMessage", () => {
component.saveMug(true);
expect(messageService.showMessage).toHaveBeenCalled();
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.