Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript rxjs模拟响应到管道/水龙头中_Javascript_Angular_Unit Testing_Rxjs_Karma Jasmine - Fatal编程技术网

Javascript rxjs模拟响应到管道/水龙头中

Javascript rxjs模拟响应到管道/水龙头中,javascript,angular,unit-testing,rxjs,karma-jasmine,Javascript,Angular,Unit Testing,Rxjs,Karma Jasmine,我正在为Angular 7.1应用程序进行单元测试。我们使用rxjs 6.3.3。我的组件有一个如下所示的服务调用 this.registrationservice .createAccount(model) .pipe( tap( (resp: {}) => { this.router.navigate([url]);

我正在为Angular 7.1应用程序进行单元测试。我们使用rxjs 6.3.3。我的组件有一个如下所示的服务调用

this.registrationservice
            .createAccount(model)
            .pipe(
                tap(
                    (resp: {}) => {
                        this.router.navigate([url]);
                    },
                    (error: any) => {
                        this.isError = true;
                        this.registrationState.resetData();
                        this.resetUrl = this.registrationState.verifyStepState(RegistrationStepName.confirm);
                    }
                ),
                finalize(() => {
                    this.isLoading = false;
                })
            )
            .subscribe();
当我尝试返回错误响应时,它从未进入.tap错误处理程序?我该怎么嘲笑这件事?这是我的测验

it("Register User error", fakeAsync(() => {
        let errorResponse = {
            statusCode: "400",
            error: "no good"
        };
        userRegisterSvcStub.createAccount.and.throwError(ErrorObservable.create(errorResponse));
        let model = new UserRegisterModel({ userregistrationtypeid: modelProviderType.id, ...modelNpi, ...modelCreateAccount });
        registerDataStateSpy.getRegisterUserData.and.returnValue(model);
        registerDataStateSpy.verifyStepState.and.returnValue("../beginning");
        component.ngOnInit();
        tick();
        expect(mockRouter.navigate).not.toHaveBeenCalled();
        expect(component.resetUrl).toEqual("../beginning");
    }));

您可以使用rxjs中的throwError操作符创建一个Observable,它可以立即抛出传递给函数的任何内容。

您可以使用rxjs中的throwError操作符创建一个Observable,它可以立即抛出传递给函数的任何内容。

为Christian的答案提供更多详细信息

从您使用它的方式来看,我假设
userRegisterSvcStub
是一个间谍对象,
createAccount()
是该对象上的间谍方法?如果是,则您当前在此处使用的代码:

userRegisterSvcStub.createAccount.and.throwError(ErrorObservable.create(errorResponse));
将实际使用jasmine
throwError()
方法抛出错误,如文档所示

我想你想要的是按照克里斯蒂安的建议使用rxjs投掷器。为此,您的代码将如下所示:

import { throwError } from 'rxjs'; // up with your other imports

userRegisterSvcStub.createAccount.and.returnValue(throwError(errorResponse));

我希望这能有所帮助。

为克里斯蒂安的回答提供更多细节

从您使用它的方式来看,我假设
userRegisterSvcStub
是一个间谍对象,
createAccount()
是该对象上的间谍方法?如果是,则您当前在此处使用的代码:

userRegisterSvcStub.createAccount.and.throwError(ErrorObservable.create(errorResponse));
将实际使用jasmine
throwError()
方法抛出错误,如文档所示

我想你想要的是按照克里斯蒂安的建议使用rxjs投掷器。为此,您的代码将如下所示:

import { throwError } from 'rxjs'; // up with your other imports

userRegisterSvcStub.createAccount.and.returnValue(throwError(errorResponse));

我希望这会有所帮助。

谢谢,但不幸的是,它仍然会进入水龙头中的“成功”块…尝试找出我可以给模拟的内容,使其认识到这是一个错误,不是成功我的错…这很有效!。。。我的另一个间谍电话仍然模拟了以前的数据,所以它没有到达我的代码…需要更多的咖啡…干杯!谢谢,但不幸的是,它仍然会进入水龙头中的“Success”块…试图找出我可以给它什么样的模拟,使它认识到这是一个错误,不是Successmy bad…这很有效!。。。我的另一个间谍电话仍然模拟了以前的数据,所以它没有到达我的代码…需要更多的咖啡…干杯!