如何编写测试以确保调用了可观察对象?
示例:obj: any = {func: () => this.obs$}
我想编写一个确保obj.func返回this.obs $的测试。
我尝试了以下操作:
it('should demonstrate obs is returned', () => {
const spy = spyOn<any>(component, 'obs$').and.returnValue(of('test'));
const ret = component.obj.func();
expect(spy).toHaveBeenCalled();
// 2nd attempt
expect(ret).toEqual('test');
})
这些都不起作用。任何帮助将不胜感激。
答案 0 :(得分:1)
您不能spyOn
obs$
假定它是可观察的而不是函数。您只能监视功能。
尝试:
it('should demonstrate obs is returned', (done) => { // add done callback here
component.obs$ = of('test'); // assign obs$ to observable of 'test'
const ret = component.obj.func();
ret.subscribe(value => { // subscribe to the observable that previous function gives.
expect(value).toBe('test');
done(); // call done to tell Jasmine that you are done with the tests
});
});
答案 1 :(得分:0)
想要回答一个单独的答案,尽管AliF50的答案很有效,并且感谢您的帮助,但他们对间谍并不正确。它也与间谍一起工作,意思是:
import os
from azure.storage.blob import BlobServiceClient, ContentSettings
AZURE = os.getenv('AZURE_STORAGE_CONNECTION_STRING')
blob_service_client = BlobServiceClient.from_connection_string(AZURE)
cnt_settings = ContentSettings(content_type="text/plain")
with open("my_file", 'rb') as f:
blob_client = blob_service_client.get_blob_client(container="my_container",
blob="my_file")
# I tried:
# 1. blob_client.set_http_headers(cnt_settings)
# 2. blob_client.upload_blob(f, **cnt_settings)
blob_client.upload_blob(f)
也可以。
但是,我发现了另一个可行的解决方案,并且更短:
it('should demonstrate obs is returned', (done) => { // add done callback here
spyOn<any>(component, 'obs$').and.returnValue(of('test'));
const ret = component.obj.func();
ret.subscribe(value => {
expect(value).toEqual('test');
done();
});
});
我的错误试图这样做:
it('should demonstrate obs is returned', () => {
expect(component.obj.func()).toEqual(component.obs$);
})
两者都不起作用。
希望这对其他人有帮助。