Jump to content

CakePHP - Whats the point of "testing"


CrimpJiggler

Recommended Posts

I read the chapter on testing in the guide, but I'm still having trouble understanding. To learn new things, I like to implement them into my project, so I setup a test case for a page I'm having trouble with, I used "cake bake" to setup all the fixtures it requires, and heres the test I added:

	public function testEdit($id = NULL) {
		
		$substanceList['slug'] = array('substances_category','compound','plant','ailment','product','preparation');
		$substanceList['model'] = array_map(function ($value) { return Inflector::classify($value); },$substanceList['slug']);
		
		foreach($substanceList['model'] as $extraModel) {

			$rdata[$extraModel] = ClassRegistry::init($extraModel)->find('list',array('order' => 'name'));
	
		}
		
		$expected = 0;
		$result = count(array_intersect($rdata['Compound'],$rdata['Product']));
		
		$this->assertEquals($expected, $result);
		
	}

thats the method in my controller which is causing a glitch, the glitch is that when I try to load a list of items from the products table, it loads a list of compounds instead, so it loads compounds twice for some reason. I setup a test, which asserts that the list of compounds doesn't contain the same items as the products list. So I go to test.php and run the test for this controller and:

5r04.png

so the problem that occured in the real situation, didn't occur in the test case. So I'm struggling to see what the purpose of testing is. Do I need to setup the code in test case, so that its pretty much identical to the actual controller? 

 

One other thing: I installed xdebug so I can "analyze code coverage" but when I click analyze code coverage, it just says "

No files to generate coverage for

what does that mean? 

Link to comment
Share on other sites

According to your new slavemaster, you must test everything. So to answer your question, yes. In the real world of getting things done, maybe not. As the dev, I think you have to decide when a test is worth doing. Remember, there is more to testing than unit testing. All your functions can be perfect, but if they don't accomplish the end goal, your testing was worthless.

Link to comment
Share on other sites

So I'm struggling to see what the purpose of testing is.

To verify it works as expected. It's also used to replicate bugs, the fix will make the test pass and thus fixes the bug. Of course this is assuming the programmer responsible wrote good tests. All you have verified is that the Compound model does not return Product models and it doesn't so your understanding of the problem is wrong.

 

So, perhaps you should review your code and find what is really going on and then write a new test matching your new findings and make sure the test fails (meaning you replicated the bug in your test). Then make your changes to your code and the end-result should be that your test succeeds.

Edited by ignace
Link to comment
Share on other sites

What kinda tests should I be setting up? For example, I made a web spider Helper which downloads wiki pages and extracts info from it, what kinda tests should I set up for that? One problem with it is if it gets pointed to a wiki page that doesn't exist, or else one that doesn't have the kinda data its searching for, then it can't extract any data and things go wrong. Should I make a test that asserts that the variable containing the extracted data is not empty? I suppose this would be useful if I set up a series of mini tests like this, that way when something goes wrong it'll help me narrow down where the problem is. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.