我有一个要导出的功能:
//Check policy connection
$ az sql server conn-policy show -g myResourceGroup -s mySQLServer
//This displayed next:
{
"connectionType": "Default",
"id": "/subscriptions/{mySusbcriptionId}",
"kind": null,
"location": "East US",
"name": "default",
"resourceGroup": "MyresourceGroup",
"type": "Microsoft.Sql/servers/connectionPolicies"
}
然后我有了另一个使用此功能的类(Mobx存储)。
$ az sql server conn-policy update -g myResourceGroup -s mySQLServer -t Proxy
我想用笑话来测试它,模拟isAppPublicRequest导入:
import HttpClientApi from "../../../common/services/httpClient/httpClient";
export const isAppPublicRequest = async () => {
try {
const appTypeRequest = await HttpClientApi.httpClient.get("system/appType");
return appTypeRequest.data;
} catch (e) {
console.error(e);
return undefined;
}
};
但是这不起作用,因为我遇到XMLHttpRequest.open错误-因此使用了初始导入,并且实际上调用了该函数。
如果我尝试将函数导入文件并用import { observable, action, computed, toJS } from "mobx";
import { isAppPublicRequest } from "../../services/auth/auth";
class AuthStore {
@observable publicMode = true;
@action
setPublicMode(isPublic) {
this.publicMode = isPublic;
}
@action
async loadIsPublic() {
const appType = await isAppPublicRequest();
if (appType === "javni_vpogled") {
this.setPublicMode(true);
} else if (appType === "ev_prod") {
this.setPublicMode(false);
}
}
}
export default AuthStore;
覆盖,则会收到一个错误,指出该变量不可设置。
如何在测试中模拟import AuthStore from "./AuthStore";
describe("authStore", () => {
it("loadIsPublic calls setPublicMode with true when appType is javni_vpogled", () => {
const mockIsPublicRequest = {
isAppPublicRequest: jest.fn(() => {
return "javni_vpogled";
})
};
jest.mock("../../services/auth/auth", () => mockIsPublicRequest);
const authStore = new AuthStore();
const setPublicModeSpy = jest.spyOn(authStore, "setPublicMode");
authStore.loadIsPublic();
expect(setPublicModeSpy).toHaveBeenCalledWith(true);
});
});
函数(我还需要它返回测试的自定义字符串)