Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.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
Angular jasmine stub可以抛出一个对象而不是字符串_Angular_Jasmine_Karma Jasmine - Fatal编程技术网

Angular jasmine stub可以抛出一个对象而不是字符串

Angular jasmine stub可以抛出一个对象而不是字符串,angular,jasmine,karma-jasmine,Angular,Jasmine,Karma Jasmine,我正在对一个组件进行单元测试。在这里,我从外部服务调用一个函数,并人为地抛出一个错误,以测试组件如何处理错误 要做到这一点,我需要我的错误是这样的 {statusCode:401,代码:“登录失败”} 但是,Jasmine throwError方法只抛出作为参数传递的单个字符串。是否有一种解决方法或方法我不知道,所以我可以抛出一个对象 以下是我目前的代码: fit('should display error message if login failure', () => { //

我正在对一个组件进行单元测试。在这里,我从外部服务调用一个函数,并人为地抛出一个错误,以测试组件如何处理错误

要做到这一点,我需要我的错误是这样的
{statusCode:401,代码:“登录失败”}

但是,Jasmine throwError方法只抛出作为参数传递的单个字符串。是否有一种解决方法或方法我不知道,所以我可以抛出一个对象

以下是我目前的代码:

fit('should display error message if login failure', () => {
    // should throw ({ statusCode: 401, code: 'LOGIN_FAILED' });
    const spyLogin = spyOn(appUserApi, 'loginUser').and.throwError('test');  
    fillForm('toto@gmail.com', '1234');
    submitForm();
    // expect stuff here
  });

我找到了一个解决方案:使用returnValue()而不是存根中的throwError() 然后在返回值中,您可以使用rxjs throwError()抛出您想要的任何东西

重要提示:这里,throwError()来自rxjs,而不是jasmine spy

spyOn(appUserApi, 'loginDoctor')
      .and
      .returnValue(
        throwError({ statusCode: 401, code: 'LOGIN_FAILED' })
      );