我正在尝试为React-Native-Keychain的辅助函数编写单元测试。
以下是辅助功能:
import {
setGenericPassword,
getGenericPassword,
resetGenericPassword
} from "react-native-keychain";
export const setToken = token => setGenericPassword("session", token);
export const getToken = () => getGenericPassword().then(creds => creds.password);
export const clearToken = () => resetGenericPassword();
这是我的测试:
import * as keyChainFunctions from "react-native-keychain";
import { setToken, getToken, clearToken } from "./secureStorage";
const token = "abcdefghijklmnopqrstuvwxyz0123456789";
jest.mock("react-native-keychain", () => {
const token = "abcdefghijklmnopqrstuvwxyz0123456789";
const credentials = {
username: "session",
token: token
};
return {
setGenericPassword: jest.fn(
(username, password) => new Promise((resolve, reject) => resolve(true)) // eslint-disable-line no-unused-vars
),
getGenericPassword: jest.fn(() => new Promise((resolve, reject) => resolve(credentials))), // eslint-disable-line no-unused-vars
resetGenericPassword: jest.fn(() => new Promise((resolve, reject) => resolve(true))) // eslint-disable-line no-unused-vars
};
});
describe("set token", () => {
it("saves the token in the keychain", () => {
expect.assertions(1);
return setToken(token).then(() => {
expect(keyChainFunctions.setGenericPassword).toHaveBeenCalledWith("session", token);
});
});
});
describe("get token", () => {
it("retrieves the token from keychain", () => {
expect.assertions(1);
return getToken().then(() => {
expect(keyChainFunctions.getGenericPassword).toHaveBeenCalled();
});
});
});
describe("clear token", () => {
it("removes the token from the keychain", () => {
expect.assertions(1);
return clearToken().then(() => {
expect(keyChainFunctions.resetGenericPassword).toHaveBeenCalled();
});
});
});
如您所见,我尝试从React-Native-Keychain模拟函数。但是这些测试失败了,因为他们说没有断言发生。但是我必须写这些断言,因为辅助函数是(/应该是)异步的。
我应该如何模拟keyChainFunctions?还是有更好的方法来测试这些辅助功能?
正如Xarvalus要求的那样,以下是错误:
npm test -t secureStorage
> ordersome@0.0.1 test /Users/jan/Startup/react-native/ordersome
> jest "secureStorage"
FAIL app/api/secureStorage/secureStorage.test.js
set token
✕ saves the token in the keychain (60ms)
get token
✕ retrieves the token from keychain (1ms)
clear token
✕ removes the token from the keychain (1ms)
● set token › saves the token in the keychain
expect(jest.fn())[.not].toHaveBeenCalledWith()
jest.fn() value must be a mock function or spy.
Received: undefined
23 | expect.assertions(1);
24 | return setToken(token).then(() => {
> 25 | expect(keyChainFunctions.setGenericPassword).toHaveBeenCalledWith("session", token);
| ^
26 | });
27 | });
28 | });
at app/api/secureStorage/secureStorage.test.js:25:52
at tryCallOne (node_modules/promise/lib/core.js:37:12)
at node_modules/promise/lib/core.js:123:15
at flush (node_modules/asap/raw.js:50:29)
● set token › saves the token in the keychain
expect.assertions(1)
Expected one assertion to be called but received zero assertion calls.
21 | describe("set token", () => {
22 | it("saves the token in the keychain", () => {
> 23 | expect.assertions(1);
| ^
24 | return setToken(token).then(() => {
25 | expect(keyChainFunctions.setGenericPassword).toHaveBeenCalledWith("session", token);
26 | });
at Object.<anonymous> (app/api/secureStorage/secureStorage.test.js:23:12)
● get token › retrieves the token from keychain
expect(jest.fn())[.not].toHaveBeenCalled()
jest.fn() value must be a mock function or spy.
Received: undefined
32 | expect.assertions(1);
33 | return getToken().then(() => {
> 34 | expect(keyChainFunctions.getGenericPassword).toHaveBeenCalled();
| ^
35 | });
36 | });
37 | });
at app/api/secureStorage/secureStorage.test.js:34:52
at tryCallOne (node_modules/promise/lib/core.js:37:12)
at node_modules/promise/lib/core.js:123:15
at flush (node_modules/asap/raw.js:50:29)
● get token › retrieves the token from keychain
expect.assertions(1)
Expected one assertion to be called but received zero assertion calls.
30 | describe("get token", () => {
31 | it("retrieves the token from keychain", () => {
> 32 | expect.assertions(1);
| ^
33 | return getToken().then(() => {
34 | expect(keyChainFunctions.getGenericPassword).toHaveBeenCalled();
35 | });
at Object.<anonymous> (app/api/secureStorage/secureStorage.test.js:32:12)
● clear token › removes the token from the keychain
expect(jest.fn())[.not].toHaveBeenCalled()
jest.fn() value must be a mock function or spy.
Received: undefined
41 | expect.assertions(1);
42 | return clearToken().then(() => {
> 43 | expect(keyChainFunctions.resetGenericPassword).toHaveBeenCalled();
| ^
44 | });
45 | });
46 | });
at app/api/secureStorage/secureStorage.test.js:43:54
at tryCallOne (node_modules/promise/lib/core.js:37:12)
at node_modules/promise/lib/core.js:123:15
at flush (node_modules/asap/raw.js:50:29)
● clear token › removes the token from the keychain
expect.assertions(1)
Expected one assertion to be called but received zero assertion calls.
39 | describe("clear token", () => {
40 | it("removes the token from the keychain", () => {
> 41 | expect.assertions(1);
| ^
42 | return clearToken().then(() => {
43 | expect(keyChainFunctions.resetGenericPassword).toHaveBeenCalled();
44 | });
at Object.<anonymous> (app/api/secureStorage/secureStorage.test.js:41:12)
Test Suites: 1 failed, 1 total
Tests: 3 failed, 3 total
Snapshots: 0 total
Time: 1.961s
Ran all test suites matching /secureStorage/i.
npm ERR! Test failed. See above for more details.