为什么不叫茉莉花茉莉

时间:2019-07-08 18:10:35

标签: javascript angular jasmine

即使调用它,我的测试也失败了。我正在监视PrimeNG消息服务。

下面是我到目前为止的一些代码。

    it('should throw error', () => {
      const mockCall = spyOn(service, 'update$').and.throwError('Error');
      const msgService = spyOn(messageService, 'add').and.callThrough();
      expect(mockCall).toThrowError('Error');
      expect(msgService).toHaveBeenCalled();
    });

我希望它能通过此测试,因为如果有错误,它将以我的update $可观察到的方式被调用。这是错误 “预期的间谍添加已被调用”

1 个答案:

答案 0 :(得分:0)

根据经验,不应将模拟应用于正在测试的方法。您应该直接调用要测试的方法,然后验证发生了什么。您还应该避免验证模拟。您已设置PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); mAuth.getCurrentUser().updatePhoneNumber(credential).addOnCompleteListener(this, new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { Log.d(TAG, "signInWithCredential:success"); Snackbar.make(findViewById(android.R.id.content), "Mobile Verified Successfully.", Snackbar.LENGTH_SHORT).show(); } else { Log.w(TAG, "signInWithCredential:failure", task.getException()); if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) { //mVerificationField.setError("Invalid code."); Snackbar.make(findViewById(android.R.id.content), "Invalid Code.", Snackbar.LENGTH_SHORT).show(); } else { Log.d("signInWithCredential", task.getException().toString()); } } } }); PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code); mAuth.getCurrentUser().linkWithCredential(credential) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { Log.d(TAG, "linkWithCredential:success"); Toast.makeText(PhoneAuthActivity.this, "linked accounts", Toast.LENGTH_SHORT).show(); FirebaseUser user = task.getResult().getUser(); // updateUI(user); } else { Log.w(TAG, "linkWithCredential:failure", task.getException()); Toast.makeText(PhoneAuthActivity.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); // updateUI(null); } // ... } }); 进行抛出,这就是它的作用,您无需验证它是否发生。

您的测试应该看起来像这样:

service.update$