如何对从数组中删除重复项的方法进行单元测试?
How to unit test a method that removes duplicates from an array?
我有以下方法根据给定的 属性 从数组中删除重复项:
removeDuplicates(myArr, prop) {
return myArr.filter((object, pos, arr) => {
return arr.map(obj => obj[prop]).indexOf(object[prop]) === pos;
});
}
现在我需要对该方法进行单元测试,但我不知道如何进行。
describe('remove duplicates', () => {
it('should remove duplicated objects from an array based on a property', () => {
//..
}
});
如何正确测试这样的方法?
您导入 removeDuplicates
函数。
您可以遵循 AAA 模式(Arrange Act Assert)。
describe('removeDuplicates', () => {
const fixtureComponent = TestBed.createComponent(YourComponent);
it('should remove objects with same given property', () => {
// Arrange
const persons = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Paul' },
{ id: 3, name: 'Ron' },
{ id: 4, name: 'John' },
{ id: 5, name: 'Louis' },
];
// Act
const distinctPersons = fixtureComponent.removeDuplicates(persons, 'name');
// Assert
expect(distinctPersons).toEqual([
{ id: 1, name: 'John' },
{ id: 2, name: 'Paul' },
{ id: 3, name: 'Ron' },
{ id: 5, name: 'Louis' },
]);
}
});
如果这是一项服务,试试这个
describe('removeDuplicates', () => {
beforeEach(() => TestBed.configureTestingModule({
providers: [
YourServiceService
]
}));
it('should remove objects with same given property', () => {
const service: YourServiceService = TestBed.get(YourServiceService );
const persons = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Paul' },
{ id: 3, name: 'Ron' },
{ id: 4, name: 'John' },
{ id: 5, name: 'Louis' },
];
let results = service.removeDuplicates(persons, 'name' );
expect(results ).toBe([
{ id: 1, name: 'John' },
{ id: 2, name: 'Paul' },
{ id: 3, name: 'Ron' },
{ id: 5, name: 'Louis' },
]);
})
})
我有以下方法根据给定的 属性 从数组中删除重复项:
removeDuplicates(myArr, prop) {
return myArr.filter((object, pos, arr) => {
return arr.map(obj => obj[prop]).indexOf(object[prop]) === pos;
});
}
现在我需要对该方法进行单元测试,但我不知道如何进行。
describe('remove duplicates', () => {
it('should remove duplicated objects from an array based on a property', () => {
//..
}
});
如何正确测试这样的方法?
您导入 removeDuplicates
函数。
您可以遵循 AAA 模式(Arrange Act Assert)。
describe('removeDuplicates', () => {
const fixtureComponent = TestBed.createComponent(YourComponent);
it('should remove objects with same given property', () => {
// Arrange
const persons = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Paul' },
{ id: 3, name: 'Ron' },
{ id: 4, name: 'John' },
{ id: 5, name: 'Louis' },
];
// Act
const distinctPersons = fixtureComponent.removeDuplicates(persons, 'name');
// Assert
expect(distinctPersons).toEqual([
{ id: 1, name: 'John' },
{ id: 2, name: 'Paul' },
{ id: 3, name: 'Ron' },
{ id: 5, name: 'Louis' },
]);
}
});
如果这是一项服务,试试这个
describe('removeDuplicates', () => {
beforeEach(() => TestBed.configureTestingModule({
providers: [
YourServiceService
]
}));
it('should remove objects with same given property', () => {
const service: YourServiceService = TestBed.get(YourServiceService );
const persons = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Paul' },
{ id: 3, name: 'Ron' },
{ id: 4, name: 'John' },
{ id: 5, name: 'Louis' },
];
let results = service.removeDuplicates(persons, 'name' );
expect(results ).toBe([
{ id: 1, name: 'John' },
{ id: 2, name: 'Paul' },
{ id: 3, name: 'Ron' },
{ id: 5, name: 'Louis' },
]);
})
})