Timeline for Where is the line between unit testing application logic and distrusting language constructs?
Current License: CC BY-SA 3.0
24 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 8, 2018 at 0:58 | comment | added | user949300 |
Since the incoming argument is named people, I simply can't imagine the code being "accidently" changed to only save one. You'd have to make two mistakes, removing the forEach and also adding a [0] .
|
|
Jul 20, 2016 at 9:27 | audit | First posts | |||
Jul 20, 2016 at 9:27 | |||||
Jun 24, 2016 at 16:56 | comment | added | Jonah |
@Falco, It's not clear what the "contract" is (please refer to my last createNewUser example in further discussion). You can argue either 1) createNewUser promises to save a user and fraud record, and send an email or 2) that the actual creation of databases records is the the domain of the dataStore, and should be tested there, and similarly for the actual sending of emails, and createNewUser just delegates. If you argue for 1), why decompose your system at all, if you are going to repeat the same tests at every level of abstraction? That's integration, not unit, testing.
|
|
Jun 24, 2016 at 14:20 | comment | added | Falco |
You have to wrap your head around this: A unit test should not check if the code does what the developer thinks it should do after reading the code. A Unit test should check the promised contract That could be "produces cookies with x,y,z attributes" or "you can query these persons in the database after this method completed" - because maybe just saving them all is not enough and you need to commit, or refresh something, mark cache stale...
|
|
Jun 24, 2016 at 0:07 | comment | added | David Moles |
"It's hard for me to imagine any kind of unit test that doesn't simply mock dough, pan, and oven, and then assert that methods are called on them." Then write that test. At minimum, you have a regression test making sure that later changes don't break the code. Beyond that, there's slebetman's answer. And beyond that, there's refactoring Oven into a more OO class that accepts a pan full of cookies and bakes it (rejecting an empty pan).
|
|
Jun 22, 2016 at 21:51 | comment | added | Leliel | @Jonah the details of that is very specific to the function you actually have to test. It should look like it's checking the contract was fulfilled though, whatever the actual contract is, and whatever mocks/components are needed to do that. | |
Jun 22, 2016 at 20:59 | comment | added | Jonah | @WorldSEnder, yes. the difficult question is what the unit test should look like. | |
Jun 22, 2016 at 19:06 | comment | added | WorldSEnder | I would say it like this: unit test it with a unit test that won't change unless the contract for the function changes (in which case you should consider deprecating it and making a new function, anyway). Now the implementation of the function can change, you'll catch falsy ones with the unit test. | |
Jun 22, 2016 at 17:44 | comment | added | Jonah | @Beska Maybe so. I'll let this post run its course first though.... | |
Jun 22, 2016 at 17:31 | comment | added | Beska | @Jonah I'd be interested in that too, but that sounds like another question. | |
Jun 22, 2016 at 15:11 | comment | added | Jonah | @BryanOakley Let me know what additional info you need to make the decision. I'm really interested in how, and in what factors influence the types of tests that are appropriate. Thanks. EDIT: Also, I'm specifically interested in how to test this as a pure unit test (ie, without using a functional test database, and making assertions on what ends up inside of it) | |
Jun 22, 2016 at 14:58 | comment | added | Bryan Oakley | @Jonah: I don't have a recommendation, there's not enough info. Giving concrete answers to abstract questions is difficult. The test is conceptually simple: call it with at least three persons, and verify the data gets saved. How you do that verification is beside the point: you asked if it should be tested, not how. | |
Jun 22, 2016 at 14:22 | comment | added | Ian | @VincentSavard, but the cost/benefit of a unit test is reduce if the risk is control in anther way. | |
Jun 22, 2016 at 14:21 | comment | added | Jonah |
@BryanOakley, so do you recommend the unit test I suggested, ie, asserting that savePerson is called with the expected names on a mock? If not, what unit test do you recommend? And if you have time, I'd love your input on my update 2 question. Thanks.
|
|
Jun 22, 2016 at 12:43 | comment | added | Vincent Savard | @Ian End to end tests do not replace unit tests, they are complimentary. Just because you may have an end to end test that ensures you cave save a list of people does not mean you shouldn't have a unit test to also cover it. | |
Jun 22, 2016 at 11:32 | comment | added | Ian | Unless you have a "end to end" system test that covers it. | |
Jun 22, 2016 at 7:24 | comment | added | Lovis | "You are testing that savePeople fulfills the promise it makes through its contract." This. So much this. | |
Jun 22, 2016 at 2:27 | history | edited | Robert Harvey | CC BY-SA 3.0 |
added 34 characters in body
|
Jun 22, 2016 at 2:25 | history | edited | Bryan Oakley | CC BY-SA 3.0 |
deleted 144 characters in body
|
Jun 22, 2016 at 2:25 | comment | added | Bryan Oakley | @RobertHarvey: There's a lot of gray area, and making the distinction, IMO, isn't important. You are right though -- it's not really important to test that "it calls the right functions" but rather "it does the right thing" regardless of how it does it. What is important is to test that, given a specific set of inputs to the function you get a specific set of outputs. However, I can see how that last sentence can be confusing, so I removed it. | |
Jun 22, 2016 at 2:24 | history | edited | Robert Harvey | CC BY-SA 3.0 |
added 21 characters in body
|
Jun 22, 2016 at 2:23 | history | edited | Bryan Oakley | CC BY-SA 3.0 |
added 45 characters in body
|
Jun 22, 2016 at 2:22 | comment | added | Robert Harvey |
... you aren't testing that the functions it calls work, you're testing that it calls the correct functions. -- Technically, that would make it an integration test, wouldn't it?
|
|
Jun 22, 2016 at 2:22 | history | answered | Bryan Oakley | CC BY-SA 3.0 |