# |
Apr 17th 2019, 14:54 |
Coddyx |
I've read that's it is considered a bad practice to test a private method |
# |
Apr 17th 2019, 14:54 |
Coddyx |
While you're there |
# |
Apr 17th 2019, 14:53 |
Coddyx |
french looks not so bad :P |
# |
Apr 17th 2019, 14:53 |
neon1024 |
Désolé je ne parle pas français parce que je suis britannique |
# |
Apr 17th 2019, 14:52 |
neon1024 |
I wouldn’t ever do that. I’d add more data to the fixture and then select the correct data in the test-case |
# |
Apr 17th 2019, 14:52 |
Coddyx |
actually, do you speak french? i just saw you're from strasbourg |
# |
Apr 17th 2019, 14:51 |
Coddyx |
Is there a way to inject a value in fixture for a specific test? |
# |
Apr 17th 2019, 14:50 |
Coddyx |
But |
# |
Apr 17th 2019, 14:50 |
Coddyx |
I'll try it |
# |
Apr 17th 2019, 14:49 |
Coddyx |
that I could |
# |
Apr 17th 2019, 14:48 |
neon1024 |
I guess you could do a write to the test db at the start of the test-case? |
# |
Apr 17th 2019, 14:48 |
neon1024 |
If you expose the value in your code or inject it you’d be able to test it much esier |
# |
Apr 17th 2019, 14:48 |
Coddyx |
Like so far I have $records = [ ['maintenance' => 1] ]; |
# |
Apr 17th 2019, 14:48 |
neon1024 |
Well that’s trying to solve a problem in testing by having bad code |
# |
Apr 17th 2019, 14:47 |
Coddyx |
but how can I have a record for a test and another one for the opposite? |
# |
Apr 17th 2019, 14:47 |
Coddyx |
And put the record in it |
# |
Apr 17th 2019, 14:47 |
Coddyx |
I baked one |
# |
Apr 17th 2019, 14:47 |
neon1024 |
Which fixes the value, so when you execute the controller request it can get the right value |
# |
Apr 17th 2019, 14:47 |
neon1024 |
I guess you’ll have to write a fixture then |
# |
Apr 17th 2019, 14:46 |
Coddyx |
$this->Model->get() ? |
# |
Apr 17th 2019, 14:46 |
Coddyx |
oops |
# |
Apr 17th 2019, 14:46 |
Coddyx |
could I mock $this- |
# |
Apr 17th 2019, 14:46 |
neon1024 |
Well if you cached the value, you could mock the cache |
# |
Apr 17th 2019, 14:45 |
Coddyx |
yes |
# |
Apr 17th 2019, 14:45 |
Coddyx |
but I can't find how to mock the db hit |
# |
Apr 17th 2019, 14:45 |
neon1024 |
Wow, so you hit the database for every single request to your application |
# |
Apr 17th 2019, 14:45 |
Coddyx |
So lets say I hit '/' i should assert 503 |
# |
Apr 17th 2019, 14:45 |
Coddyx |
and isInMaintenance checks against a model and returns true or false |
# |
Apr 17th 2019, 14:45 |
Coddyx |
if this->isInMaintenance throw httpexception 503 |
# |
Apr 17th 2019, 14:44 |
Coddyx |
AppController::beforeFilter |
# |
Apr 17th 2019, 14:44 |
neon1024 |
Otherwise you’d need to write a fixture for the data |
# |
Apr 17th 2019, 14:44 |
Coddyx |
My code looks like this |
# |
Apr 17th 2019, 14:44 |
neon1024 |
Or maybe a query param |
# |
Apr 17th 2019, 14:43 |
neon1024 |
Perhaps pass it into the controller |
# |
Apr 17th 2019, 14:43 |
neon1024 |
Perhaps read from a session or cookie |
# |
Apr 17th 2019, 14:43 |
neon1024 |
So you’ll need to just set that manually |
# |
Apr 17th 2019, 14:43 |
neon1024 |
Well you must use the value somewhere right? |
# |
Apr 17th 2019, 14:43 |
Coddyx |
I can show you what I have so far |
# |
Apr 17th 2019, 14:43 |
Coddyx |
I understand, can yo uplease define «set it manually» |
# |
Apr 17th 2019, 14:42 |
neon1024 |
Writing tests for bad code is super hard work |
# |
Apr 17th 2019, 14:42 |
neon1024 |
If you can’t do that then you need to refactor your code to make it more testable |