Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I test all enum values in a contract?

I have a doubt about about whether I should consider a certain type of test functional or contract.

Let's say I have an API like /getToolType, that accepts a {object" "myObject"} as input, and returns at type in the form {type: "[a-z]+"}

It was agreed between client and server that the types returned will match a set of strings, let's say [hammer|knife|screwdriver], so the consumer decided to parse them in an enum, with a fallback value when the returned type is unknown.

Should the consumer include a test case for each type(hammer, knife, screwdriver) to ensure the producer is still following the agreement that it will always return , for instance , the lowercase string "hammer" when /getToolType is called with an hammer object? Or would you consider such a test case as functional? And why?

like image 558
rrabio Avatar asked Mar 29 '17 09:03

rrabio


People also ask

Should you unit test an enum?

Unit tests have no knowledge of how your months will be used and even less knowledge of how they might be used in the future. That implies that you should include unit tests for them, even if the application won't currently compile if all the months are not present.

Should enum values be all caps?

Because they are constants, the names of an enum type's fields are in uppercase letters. You should use enum types any time you need to represent a fixed set of constants.

Should your API use enums?

In general, use enums only when they will never change, or when they are used for input and can be more resistant to a change in the list of valid values. For fields that change often, avoid enums in favor of a string combined with an operation to obtain the latest set of values with descriptions.


1 Answers

IMO the short answer is 'no'.

Contract testing is more interested in structure, if we start boundary testing the API we move into functional test [1] territory, which is best done in the provider code base. You can use a matcher to ensure only one of those three values is returned, this should ensure the Provider build can't return other values.

I would echo @J_A_X's comments - there is no right or wrong answer, just be wary of testing all permutations of input/output data.

[1] https://docs.pact.io/best_practices/contract_tests_not_functional_tests.html

like image 151
Matthew Fellows Avatar answered Dec 15 '22 00:12

Matthew Fellows