can I use this with AAA pattern? #3749
-
I like the AAA pattern a lot because it gives me a very clear picture of what the test structure looks like. However I don't know where to put the AAA comment in property based testing though, it seems Act and Assert are stuck together. test('should contain the same items', () => {
// Arrange
const count = (tab, element) => tab.filter((v) => v === element).length;
// Act ?? Assert ??
fc.assert(
fc.property(fc.array(fc.integer()), (data) => {
const sorted = sort(data);
expect(sorted.length).toEqual(data.length);
for (const item of data) {
expect(count(sorted, item)).toEqual(count(data, item));
}
})
);
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I'd say that yes AAA pattern can fit with fast-check and more generally with property based testing. Most of the time when writing such tests, the "Arrange" phase will be the part defining what are the arbitraries being used by your property. But from time to time it can be larger than just them and contains some lines included within the predicate itself. For the "Act" and "Assert", I'd say they are still applying to the same things as for normal example based tests. In other words for you test, I'd say: test('should contain the same items', () => {
fc.assert(
fc.property(
// Arrange
fc.array(fc.integer()), (data) => {
// Act
const sorted = sort(data);
// Assert
expect(sorted.length).toEqual(data.length);
for (const item of data) {
expect(count(sorted, item)).toEqual(count(data, item));
}
})
);
});
// Helpers
// I'd not include count as part of the test as it's a very generic helper.
// Or if I do I'd inline it within the predicate or at least in the Arrange part.
function count(tab, element) {
return tab.filter((v) => v === element).length;
} And I often go for: test('should contain the same items', () => {
fc.assert(
fc.property(fc.array(fc.integer()), (data) => {
// Arrange / Act
const sorted = sort(data);
// Assert
expect(sorted.length).toEqual(data.length);
for (const item of data) {
expect(count(sorted, item)).toEqual(count(data, item));
}
})
);
}); |
Beta Was this translation helpful? Give feedback.
I'd say that yes AAA pattern can fit with fast-check and more generally with property based testing.
Most of the time when writing such tests, the "Arrange" phase will be the part defining what are the arbitraries being used by your property. But from time to time it can be larger than just them and contains some lines included within the predicate itself. For the "Act" and "Assert", I'd say they are still applying to the same things as for normal example based tests.
In other words for you test, I'd say: