javascript 单元测试 - return 来自 jest 虚拟模拟的不同对象
javascript unit testing - return different objects from jest virtual mocks
我有一个使用虚拟模拟的开玩笑单元测试。
虚拟模拟 return 一个 apiName = "standard__abc"
的对象
我测试了一个使用模拟对象的函数 (isApiNameABC()) return 如果 apiName === "standard__abc" 为真,否则 return 为假。
使用下面的代码,我可以测试 returned...
的条件
我的问题是,如何修改我的测试代码,以便虚拟模拟 return 为 apiName 设置一些其他值。我想这样做是为了测试 isApiNameABC() returns false.
的情况
import * as utils from '../utils';
jest.mock('mdl/appContextService', () => {
return {
appContext: {
apiName: "standard__abc"
}
}
}, {virtual: true});
describe("utils", () => {
test("test return value of apiName is EQUAL to standard__abc", () => {
expect(utils.isApiNameABC()).toEqual(true);
});
});
apiName
只是模块导出上的 属性,因此您可以直接更改它。
假设 utils.js
看起来像这样:
import { appContext } from 'mdl/appContextService';
export const isApiNameABC = () => appContext.apiName === 'standard__abc';
你可以这样测试:
import * as utils from './utils';
import { appContext } from 'mdl/appContextService';
jest.mock('mdl/appContextService', () => {
return {
appContext: {
apiName: "standard__abc"
}
}
}, { virtual: true });
describe("utils", () => {
test("test return value of apiName is EQUAL to standard__abc", () => {
expect(utils.isApiNameABC()).toEqual(true); // Success!
});
test("test return value of apiName is NOT EQUAL to standard__abc", () => {
const original = appContext.apiName;
appContext.apiName = 'something else'; // <= change it directly
expect(utils.isApiNameABC()).toEqual(false); // Success!
appContext.apiName = original; // <= restore it
});
});
如果 apiName
是一个函数,那么您可以使用模拟函数并更改其 return 值:
import * as utils from './utils';
import { appContext } from 'mdl/appContextService';
jest.mock('mdl/appContextService', () => {
return {
appContext: {
apiName: jest.fn(() => "standard__abc") // <= use a mock function
}
}
}, { virtual: true });
describe("utils", () => {
test("test return value of apiName is EQUAL to standard__abc", () => {
expect(utils.isApiNameABC()).toEqual(true); // Success!
});
test("test return value of apiName is NOT EQUAL to standard__abc", () => {
appContext.apiName.mockReturnValue('something else'); // <= change the return value
expect(utils.isApiNameABC()).toEqual(false); // Success!
});
});
我有一个使用虚拟模拟的开玩笑单元测试。
虚拟模拟 return 一个 apiName = "standard__abc"
的对象我测试了一个使用模拟对象的函数 (isApiNameABC()) return 如果 apiName === "standard__abc" 为真,否则 return 为假。
使用下面的代码,我可以测试 returned...
的条件我的问题是,如何修改我的测试代码,以便虚拟模拟 return 为 apiName 设置一些其他值。我想这样做是为了测试 isApiNameABC() returns false.
的情况import * as utils from '../utils';
jest.mock('mdl/appContextService', () => {
return {
appContext: {
apiName: "standard__abc"
}
}
}, {virtual: true});
describe("utils", () => {
test("test return value of apiName is EQUAL to standard__abc", () => {
expect(utils.isApiNameABC()).toEqual(true);
});
});
apiName
只是模块导出上的 属性,因此您可以直接更改它。
假设 utils.js
看起来像这样:
import { appContext } from 'mdl/appContextService';
export const isApiNameABC = () => appContext.apiName === 'standard__abc';
你可以这样测试:
import * as utils from './utils';
import { appContext } from 'mdl/appContextService';
jest.mock('mdl/appContextService', () => {
return {
appContext: {
apiName: "standard__abc"
}
}
}, { virtual: true });
describe("utils", () => {
test("test return value of apiName is EQUAL to standard__abc", () => {
expect(utils.isApiNameABC()).toEqual(true); // Success!
});
test("test return value of apiName is NOT EQUAL to standard__abc", () => {
const original = appContext.apiName;
appContext.apiName = 'something else'; // <= change it directly
expect(utils.isApiNameABC()).toEqual(false); // Success!
appContext.apiName = original; // <= restore it
});
});
如果 apiName
是一个函数,那么您可以使用模拟函数并更改其 return 值:
import * as utils from './utils';
import { appContext } from 'mdl/appContextService';
jest.mock('mdl/appContextService', () => {
return {
appContext: {
apiName: jest.fn(() => "standard__abc") // <= use a mock function
}
}
}, { virtual: true });
describe("utils", () => {
test("test return value of apiName is EQUAL to standard__abc", () => {
expect(utils.isApiNameABC()).toEqual(true); // Success!
});
test("test return value of apiName is NOT EQUAL to standard__abc", () => {
appContext.apiName.mockReturnValue('something else'); // <= change the return value
expect(utils.isApiNameABC()).toEqual(false); // Success!
});
});